如何使用 php 更新数组值并将其放入 mysql 数据库中?

发布于 2025-01-01 18:07:06 字数 1686 浏览 0 评论 0原文

好吧,不久前我问了这个问题

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

响应非常完美,并且它可以很好地将值插入数据库。然而,我现在正在重新访问这个项目,我只想使用相同或类似的方法更新数据库中的一个字段,特别是该特定用户的后测试字段。这怎么可能呢?

目前我有一些与此类似的东西。

$SGUID = the Unique User ID
$SGpost = the field i want to update
$SGQID = question ID

显然,这不起作用......而且我的代码非常草率,因为我试图一路替换和编辑它。有什么想法吗?

foreach ($_POST as $p => $v)
{

 if (preg_match("/^Q\d+$/",$p)) {   //$_POST variables with Q1, Q2, Q3, etc etc

$query_3 = mysql_query("UPDATE SGresults SET SGpost = '$p', SGpostCheck = 1 WHERE SGUID = '$SGUID' AND SGQID = '$p'") or die('Error: '.mysql_error ());
    $SGQID++;

}
}
}   

当我回显 $p 时,我得到以下内容 Q11 Q21 Q31 Q44 Q54 Q63 Q73 Q83 Q93 Q103 Q111 Q122 Q131 Q142 Q153 Q163 Q17

这就是我想要的,除了没有 Q,只有第二个值。例如,如果它是“Q163”,我只想将“3”部分插入到数据库为“Q16”的 SGpost 中。

再次感谢!任何帮助将不胜感激!

编辑:: 再次查看我的代码并更改了一些值......以下内容确实有效,所以我想我会坚持下去。除非代码不好?再次感谢您的建议。

$SGQID= 1;

foreach ($_POST as $i => $v)
  {
    if (preg_match("/^Q\d+$/",$i)) {    //$_POST variables with Q1, Q2, Q3, etc etc
// Here we insert value $v to database
    if ($SGtestType == "Post-test") {

    $query_post = mysql_query("UPDATE SGresults SET SGpost = '$v', SGpostcheck ='1' WHERE SGQID = '$SGQID' AND SGUID = '$SGUID'  ") or die('Error 109: '.mysql_error ());
    $SGQID++;           
    // ^^ increments the question value everytime an insert is made
    //last 2 fields is check for existing value.. or if not exists.. 1 if it does.
    }

}

}
}

Okay so a while ago I asked this question

How can I grab and store multiple $_POST Variables in a for each loop?

And the response was perfect, and it worked for inserting the values into the database just fine. However, I now am revisiting this project and I want to UPDATE only one field in the database, specifically the post test field for that particular user, using the same or similar method. How would this be possible?

Currently I have something along the lines of this.

$SGUID = the Unique User ID
$SGpost = the field i want to update
$SGQID = question ID

Obviously, this does not work.. and my code is extremely sloppy as I tried to replace and edit this along the way. Any ideas?

foreach ($_POST as $p => $v)
{

 if (preg_match("/^Q\d+$/",$p)) {   //$_POST variables with Q1, Q2, Q3, etc etc

$query_3 = mysql_query("UPDATE SGresults SET SGpost = '$p', SGpostCheck = 1 WHERE SGUID = '$SGUID' AND SGQID = '$p'") or die('Error: '.mysql_error ());
    $SGQID++;

}
}
}   

When I echo out $p I get the following
Q11
Q21
Q31
Q44
Q54
Q63
Q73
Q83
Q93
Q103
Q111
Q122
Q131
Q142
Q153
Q163
Q17

Which is what I want, except without the Q, and just the second value. So for example if it was 'Q163' I just want to insert the '3' part into SGpost WHERE the database is 'Q16'.

Thanks again again! Any help would be greatly appreciated!

EDIT::
Looked over my code again and changed some values around...The following DOES work, so I think I will stick with it. Unless bad code? Thanks again for your suggestions.

$SGQID= 1;

foreach ($_POST as $i => $v)
  {
    if (preg_match("/^Q\d+$/",$i)) {    //$_POST variables with Q1, Q2, Q3, etc etc
// Here we insert value $v to database
    if ($SGtestType == "Post-test") {

    $query_post = mysql_query("UPDATE SGresults SET SGpost = '$v', SGpostcheck ='1' WHERE SGQID = '$SGQID' AND SGUID = '$SGUID'  ") or die('Error 109: '.mysql_error ());
    $SGQID++;           
    // ^^ increments the question value everytime an insert is made
    //last 2 fields is check for existing value.. or if not exists.. 1 if it does.
    }

}

}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

一城柳絮吹成雪 2025-01-08 18:07:07

假设您相信发布数据不是 xss 攻击:

    foreach ($_POST as $p => $v)
    {

     if (preg_match("/^Q(\d+)$/",$p, $matches)) {   //$_POST variables with Q1, Q2, Q3, etc etc

$id = (int)$matches[1];

    $query_3 = mysql_query("UPDATE SGresults SET SGpost = '$id', SGpostCheck = 1 WHERE SGUID = '$SGUID' AND SGQID = '$id'") or die('Error: '.mysql_error ());
        $SGQID++;

    }
    }
    }

Assuming you trust the post data to not be an xss attack:

    foreach ($_POST as $p => $v)
    {

     if (preg_match("/^Q(\d+)$/",$p, $matches)) {   //$_POST variables with Q1, Q2, Q3, etc etc

$id = (int)$matches[1];

    $query_3 = mysql_query("UPDATE SGresults SET SGpost = '$id', SGpostCheck = 1 WHERE SGUID = '$SGUID' AND SGQID = '$id'") or die('Error: '.mysql_error ());
        $SGQID++;

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