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

PDO Prepared statement insert 1 instead of string

我有这个查询,无论 $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更新为布尔值(0/1)。

也许您想使用 ,

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