我如何获取和存储多个$ _POST变量在每个循环? [英] How can I grab and store multiple $_POST Variables in a for each loop?
问题描述
所以举例..
$ demoEMP = $ _POST ['demoEMP'];
这只是将具体问题的答案储存在一个变量中。
我想要做的是在POST变量中存储问题1-20,并将它们插入到数据库中。
我不想要做的是写20条插入命令或20条$ var = $ _POST ['var']
所以我在考虑一个for循环会做的伎俩...这是我迄今为止...基于在看各种方法和教程。 ($ i = 0; $ i <55; $ i ++){
$ answer = $ /
Q {$ I};
echo $ _POST [$ answer];
}
我是否在正确的轨道上?感谢您的帮助!
UPDATE ::
Kas'解决方案非常棒。
像这样... ...
$ $ $ $ $ $ $ $ $ $ $ $ $ b if(preg_match(/ ^ Q\d + $ /,$ i)){
//这里我们插入值$ v到数据库
$ query_2 = mysql_query( INSERT INTO SGresult VALUES('','','$ pID','$ SGQID','$ v'))或死('Error:'.mysql_error());
}
}
然而,我现在试图找出一个方法来添加一个ID的问题,并有ID自动递增...每个插入。我以为我可以做一些像SGQID ++的东西,但似乎没有工作..任何人有任何想法?
再次感谢!
好了,经过一番修改之后,似乎++的位置已经关闭了,它需要在查询之后...正确的代码如下。
$ SGQID = 1;
foreach($ _POST as $ i => $ v)
{
if(preg_match(/ ^ Q\d + $ /,$ i)){
//在这里,我们插入值$ v到数据库
$ query_2 = mysql_query(INSERT INTO SGresult VALUES('','','$ pID','$ SGQID','$ v'))或者die('Error:'.mysql_error());
$ SGQID ++; //增加的值增加在DB
if(preg_match(/ ^ Q\ d + $ /,$ i)){
//这里我们插入值$ v到数据库
}
}
So currently I am using a 3rd party software application to get results from various surveys. This application is sending the results to a page via a $_POST variable.
So for example..
$demoEMP = $_POST['demoEMP'];
This just stores the answer from the specific question into a variable.
What I want to do is store Questions 1-20 in POST variables and Insert them into a database.. What I don't want to do is write 20+ Insert commands or 20+ $var = $_POST['var']
So I was thinking a for loop would do the trick... This is what I have so far... based on looking at various methods and tutorials. =/
for ($i = 0; $i < 55; $i++) {
$answer = "Q{$i}";
echo $_POST[$answer];
}
Am I even on the right track? Thanks for your help!
UPDATE:: Kas' solution worked great.
I used something like this...
foreach ($_POST as $i => $v)
{
if (preg_match("/^Q\d+$/",$i)) {
// Here we insert value $v to database
$query_2 = mysql_query("INSERT INTO SGresult VALUES ('', '', '$pID', '$SGQID','$v')") or die('Error: '.mysql_error ());
} }
However, I am now trying to figure a way to add an ID to the question and have that ID auto increment... on every insert. I thought I could do something like $SGQID++ but that does not seem to be working.. anyone have any thoughts?
Thanks again!
okay after some more tinkering it seems the placement of the ++ was off and it needed to be after the query... the correct code is as follows.
$SGQID= 1;
foreach ($_POST as $i => $v)
{
if (preg_match("/^Q\d+$/",$i)) {
// Here we insert value $v to database
$query_2 = mysql_query("INSERT INTO SGresult VALUES ('', '', '$pID', '$SGQID','$v')") or die('Error: '.mysql_error ());
$SGQID++; //added value to increment Question ID in DB
}
}
foreach ($_POST as $i => $v) {
if (preg_match("/^Q\d+$/",$i)) {
// Here we insert value $v to database
}
}
这篇关于我如何获取和存储多个$ _POST变量在每个循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!