PDO error: "Invalid parameter number: parameter not defined"

I am trying to use a simple MySQL insert query with parameters in the form of an array. He continues to tell me that the number of parameters is incorrect. I 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 columns to avoid null insertion:

 $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 usually use mysqli, but my current shared host does not have the mysqlnd plugin, preventing me from using prepare (), so any insight is appreciated from this point of view.

+4
source share
2 answers

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

The array key for the value that you pass for this token when you execute() is called :color: Remove the final one : (I assume it was just a typo).

 $stmt3->execute(array( ':room' => $Clean['room'], ':name' => $Clean['name'], ':message' => $Clean['message'], ':time' => $time, ':color' => $Clean['color'], )); 
+18
source

Maybe I'm wrong, 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 always did it like that

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

All Articles