gpt4 book ai didi

php - PDO 准备语句插入 1 而不是字符串

转载 作者:行者123 更新时间:2023-11-29 06:26:43 33 4
gpt4 key购买 nike

我有这个查询,无论 $relocation['persons_id'] 是什么,都会将 residents 更新为 1

下面的代码将在这个例子中回显 11,13,但是将 residents 设置为 1:

    $query = $db->prepare('UPDATE `apartments` SET `residents` = :persons_id AND `occupation_date` = :occupation_date WHERE `id` = :apartments_id');
echo $relocation['persons_id']."<br>\n";
$query->bindParam(':persons_id', $relocation['persons_id']);
$query->bindParam(':occupation_date', $relocation['occupation_date']);
$query->bindParam(':apartments_id', $relocation['apartments_id']);
$query->execute();

residents 字段的数据类型为 varchar(200)

你能解释一下我做错了什么吗?

最佳答案

问题就出在这里

SET `residents` = :persons_id AND `occupation_date` = :occupation_date

这意味着,对于运算符优先级

UPDATE `apartments` SET `residents` = (:persons_id AND `occupation_date` = :occupation_date) WHERE `id` = :apartments_id

因此 residents 更新为 bool 值 (0/1)。

也许你想使用,

UPDATE `apartments` SET `residents` = :persons_id, `occupation_date` = :occupation_date WHERE `id` = :apartments_id

关于php - PDO 准备语句插入 1 而不是字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30560292/

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