如何在 foreach 循环中获取并存储多个 $_POST 变量?

发布于 2024-12-10 11:51:54 字数 1250 浏览 0 评论 0原文

因此,目前我正在使用第三方软件应用程序来获取各种调查的结果。该应用程序通过 $_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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浮萍、无处依 2024-12-17 11:51:54
foreach ($_POST as $i => $v) {
  if (preg_match("/^Q\d+$/",$i)) {
    // Here we insert value $v to database
  }
}
foreach ($_POST as $i => $v) {
  if (preg_match("/^Q\d+$/",$i)) {
    // Here we insert value $v to database
  }
}
三五鸿雁 2024-12-17 11:51:54

您可以循环 $_POST 数组本身:

foreach ($_POST as $key => $value) {
    // Filter on value of $key
    // In this case only keep variables that start with a Q
    if (! substr($key, 0, 1) == 'Q') {
        continue;
    }

    // Do something with value
    echo $_POST[$key];     
}

如果您想将值分配给与 $_POST 数组的键同名的 PHP 变量,您可以使用 变量

$key = $value

$_POST['demoEMP'] 的值将被赋值给 $demoEMP。

You can loop over the $_POST array itself:

foreach ($_POST as $key => $value) {
    // Filter on value of $key
    // In this case only keep variables that start with a Q
    if (! substr($key, 0, 1) == 'Q') {
        continue;
    }

    // Do something with value
    echo $_POST[$key];     
}

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:

$key = $value

The value of $_POST['demoEMP'] will then be assigned to $demoEMP.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文