如何在 foreach 循环中获取并存储多个 $_POST 变量?
因此,目前我正在使用第三方软件应用程序来获取各种调查的结果。该应用程序通过 $_POST 变量将结果发送到页面。
例如......
$demoEMP = $_POST['demoEMP'];
这只是将特定问题的答案存储到变量中。
我想要做的是将问题 1-20 存储在 POST 变量中并将它们插入数据库中。 我不想做的是编写 20+ 插入命令或 20+ $var = $_POST['var']
所以我想 for 循环就可以解决问题...这就是我到目前为止所拥有的..基于查看各种方法和教程。 =/
for ($i = 0; $i < 55; $i++) {
$answer = "Q{$i}";
echo $_POST[$answer];
}
我走在正确的轨道上吗?感谢您的帮助!
更新:: 卡斯的解决方案效果很好。
我用过这样的东西......
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 ());
} 但是
,我现在正在尝试找到一种方法来向问题添加 ID 并让该 ID 在每次插入时自动递增。我以为我可以做类似 $SGQID++ 的事情,但这似乎不起作用..有人有什么想法吗?
再次感谢!
好吧,经过一番修改后, ++ 的位置似乎已关闭,它需要位于查询之后......正确的代码如下。
$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
}
}
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
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以循环 $_POST 数组本身:
如果您想将值分配给与 $_POST 数组的键同名的 PHP 变量,您可以使用 变量:
$_POST['demoEMP'] 的值将被赋值给 $demoEMP。
You can loop over the $_POST array itself:
If you want to assign the values to PHP variables with the same name as the key of the $_POST array you can use variable variables:
The value of $_POST['demoEMP'] will then be assigned to $demoEMP.