php-mysql-pdo:插入时准备后执行不工作


php-mysql-pdo: execute not working after prepare when inserting

我有以下行:

    $sql = "INSERT INTO news (title, content) VALUES :title, :content";
    $pre = $this->prepare($sql);
    $pre->bindValue(":title", "xxx");
    $pre->bindValue(":content", "yyy");
    $pre->execute();

我没有得到任何错误,但查询也没有执行(我查看了查询日志)。

我拼命地尝试了以下改变:

 $t="xxx" and $pre->bindValue(":title", $t); (the same also for y)
 $sql = "INSERT INTO `news` (`title`, `content`) VALUES :title, :content";
 $sql = "INSERT INTO `news` (`title`, `content`) VALUES ':title', ':content'";

没有什么变化。有趣的是,我没有得到任何回应,没有任何警告,没有任何错误。但查询不会执行。

我找到了类似的帖子,但没有一个能解决我的问题。

(关于$this…该代码在一个从PDO类扩展而来的类中。)

尝试一下,您的值应该封装在values()

"INSERT INTO news (title, content) VALUES (:title, :content)";

而不是

"INSERT INTO news (title, content) VALUES :title, :content";

尝试:"INSERT INTO news (title, content) VALUES (:title, :content)";

插入值必须用括号括起来。