6

我正在尝试使用带有数组形式参数的简单 MySQL 插入查询。它一直告诉我参数的数量是错误的。我尝试了以下方法,都产生了相同的错误:

$stmt3 = $link->prepare('INSERT INTO messages VALUES(null, :room, :name, :message, :time, :color)');
$stmt3->execute(array(':room' => $Clean['room'],':name' => $Clean['name'],':message' => $Clean['message'],':time' => $time,':color:' => $Clean['color']));

$stmt3 = $link->prepare('INSERT INTO messages VALUES(:null, :room, :name, :message, :time, :color)');
$stmt3->execute(array(':null' => null, ':room' => $Clean['room'],':name' => $Clean['name'],':message' => $Clean['message'],':time' => $time,':color:' => $Clean['color']));

以及专门声明列以避免空插入:

$stmt3 = $link->prepare('INSERT INTO messages (room, name, message, time, color) VALUES(:room, :name, :message, :time, :color)');
$stmt3->execute(array(':room' => $Clean['room'],':name' => $Clean['name'],':message' => $Clean['message'],':time' => $time,':color:' => $Clean['color']));

这是我第一次使用 PDO(我通常使用 mysqli,但我当前的共享主机没有 mysqlnd 插件,这使我无法使用 prepare(),所以从这个角度来看任何见解都是值得赞赏的。

4

2 回答 2

21

问题 - 你会踢自己 - 是与:color

调用时为该标记传递的值的数组键execute()名为 named :color:。删除尾随:(我猜这只是一个错字)。

$stmt3->execute(array(
    ':room' => $Clean['room'],
    ':name' => $Clean['name'],
    ':message' => $Clean['message'],
    ':time' => $time,
    ':color' => $Clean['color'],
    ));
于 2012-06-15T10:37:37.760 回答
-2

我可能在这里错了,但据我所知,你需要这样做:

$stmt3->bindParam(':room', $Clean['room']);
$stmt3->bindParam(':name', $Clean['name']);
//and so on

但作为个人喜好,我一直这样做

$stmt3 = $link->prepare('INSERT INTO messages VALUES(null, ?, ?, ?, ?, ?)');
$stmt3->execute(array($Clean['room'], $Clean['name'], $Clean['message'], $time, $Clean['color']))
于 2012-06-15T10:31:33.807 回答