gpt4 book ai didi

php - 在 foreach 循环中更新数据库?

转载 作者:行者123 更新时间:2023-11-29 00:27:22 25 4
gpt4 key购买 nike

在我正在创建的纸牌游戏中,我需要在 foreach 循环中更新数据库,因为每 3 张纸牌 checkbox 它都会为他们创建的手牌更新数据库。

我遇到的问题是,即使它看起来是正确的,但它不起作用。

if (isset($_POST['hand']) == true)
{
if (sizeof($_POST['checkbox']) == 3)
{
foreach($_POST['checkbox'] as $checkbox)
{
$query = "UPDATE games SET ? = ? WHERE comp = 0";
$db->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING );
$stmt = $db->prepare($query);
$stmt->bindValue(1, "ss$ver");
$stmt->bindValue(2, $checkbox);
$stmt->execute();
$ver = $ver + 1;
echo $checkbox.'<br/>';
}
}
else
{
echo 'You must only select 3 cards.';
}
}

$ver 是为了让卡片可以在 foreach 循环中完成。随着 "ss$ver"/'ss'.$ver 在每次循环时递增,因此它会跟随下一个 field title/column 在数据库中。但是我得到的 mySQL 错误是这样的。

Warning: PDOStatement::execute(): SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''ss1' = '13' WHERE comp = 0' at line 1 in E:\wamp\www\index.php on line 126

Warning: PDOStatement::execute(): SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''ss2' = '15' WHERE comp = 0' at line 1 in E:\wamp\www\index.php on line 126`

等..

关于如何绕过复选框的 foreach 循环或在循环中实际更新有什么想法吗?

感谢任何帮助!

对于表单

echo '<form input="index.php" method="post">';
foreach($fetch as $key => $value)
{
$check = '<input class="check" type="checkbox" name="checkbox[]" value="'.$value.'">';
echo $cards[$value].$check;
}

最佳答案

if (isset($_POST['hand']) == true)
{
if (sizeof($_POST['checkbox']) == 3)
{
foreach($_POST['checkbox'] as $checkbox)
{
$query = "UPDATE games SET ss$ver = ? WHERE comp = 0";
$db->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING );
$stmt = $db->prepare($query);
// $stmt->bindValue(1, 'ss'.$ver);
$stmt->bindValue(1, $checkbox);
$stmt->execute();
$ver = $ver + 1;
echo $checkbox.'<br/>';
}
}
else
{
echo 'You must only select 3 cards.';
}
}

无法在列/表名称上绑定(bind)变量。

关于php - 在 foreach 循环中更新数据库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18370880/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com