Php – theSQL PDO error

databaseMySQLpdoPHP

I am quite confused what I have done wrong in here.

My PDO statement

$sql = "INSERT INTO my_tb (stud_user, stud,pass, stud_salt) VALUES (:username, :password, :salt)";
$stmt = $db->prepare($sql);
$stmt->execute(array(':username' => $student_username,':password' => $student_password,':salt' => $student_salt));

My DB schema

CREATE TABLE IF NOT EXISTS `my_tb` (
`stud_id` int(11) NOT NULL AUTO_INCREMENT,
`stud_user` varchar(50) NOT NULL,
`stud_pass` varchar(50) NOT NULL,
`stud_salt` varchar(50) NOT NULL,
PRIMARY KEY (`stud_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

I echoed the three vars before inserting to make sure it has values. Whenever I execute this, I always end up in this error:

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[21S01]: Insert value list does not match column list: 1136 Column count doesn't match value count at row 1' in C:\Apache\htdocs\path\to\file.php:47 Stack trace: #0 C:\Apache\htdocs\path\to\file.php(47): PDOStatement->execute(Array) #1 {main} thrown in C:\Apache\htdocs\path\to\file.php on line 47

Best Answer

You have a comma between "stud" and "pass" in your insert statement - it should be

INSERT INTO my_tb (stud_user, stud_pass, stud_salt) VALUES (:username, :password, :salt)";