【问题标题】:PDO Prepared statement insert 1 instead of stringPDO Prepared 语句插入 1 而不是字符串
【发布时间】:2015-05-31 17:19:59
【问题描述】:

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

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

【问题讨论】:

  • 尝试将数据类型转换为 int (11) 并检查一次。请
  • 输出是什么:echo $relocation['persons_id']
  • @Rizier123 如问题 11,13 所述
  • @anant kumar singh 当我尝试插入一个字符串时,int 可能是不可能的。

标签: php mysql pdo prepared-statement


【解决方案1】:

问题出在这里

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

【讨论】:

  • 这似乎很正确,我必须睡一觉。我会测试并返回。
  • 我反过来体验了这个问题。在 PHP 中,AND 出现在 =(而不是 &amp;&amp;)中,这让我非常困惑,直到我阅读了文档。
猜你喜欢
  • 1970-01-01
  • 2012-03-27
  • 2012-05-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多