我有这个问题,无论$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)
。
你能解释一下我做错了吗?
答案 0 :(得分:2)
问题出在这里
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
更新为布尔值(0/1)。
也许您想要使用,
UPDATE `apartments` SET `residents` = :persons_id, `occupation_date` = :occupation_date WHERE `id` = :apartments_id