PDO Error: “ Invalid parameter number: parameter w

2019-02-22 07:54发布

I am trying to use a simple MySQL insert query with the parameters in array form. It keeps telling me the number of parameters are wrong. I have tried the following, all producing the same error:

$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']));

and

$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']));

as well as declaring the columns specifically to avoid the null insert:

$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']));

This is my first time using PDO (I normally use mysqli, but my current shared host does not have the mysqlnd plugin, preventing me from using prepare(), so any insight from that point of view is appreciated.

2条回答
Animai°情兽
2楼-- · 2019-02-22 08:32

The problem - and you will kick yourself - is with :color.

The array key for the value you are passing for that marker when calling execute() is named :color:. Remove the trailing : (I'm guessing this was just a typo anyway).

$stmt3->execute(array(
    ':room' => $Clean['room'],
    ':name' => $Clean['name'],
    ':message' => $Clean['message'],
    ':time' => $time,
    ':color' => $Clean['color'],
    ));
查看更多
祖国的老花朵
3楼-- · 2019-02-22 08:45

I might be wrong here, but as far as I know you need to do this:

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

But as a personal preference, I've always done it like this

$stmt3 = $link->prepare('INSERT INTO messages VALUES(null, ?, ?, ?, ?, ?)');
$stmt3->execute(array($Clean['room'], $Clean['name'], $Clean['message'], $time, $Clean['color']))
查看更多
登录 后发表回答