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

时间:2015-05-31 17:20:00

标签: php mysql pdo prepared-statement

我有这个问题,无论$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)

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

1 个答案:

答案 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