I am having some trouble with an insert query in php, for some reason it fails. I have echoed out the variables and they do equal something.
$query = "INSERT into thrives (n_emp, comment, g_emp) VALUES (':n_emp', ':comment', ':g_emp')";
$db -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$statement = $db -> prepare($query);
$statement -> bindValue(':n_emp', $name, PDO::PARAM_INT);
$statement -> bindValue(':comment', $comment, PDO::PARAM_STR);
$statement -> bindValue(':g_emp', $_SESSION['emplid'], PDO::PARAM_INT);
$result = $statement -> execute();
$statement -> closecursor();
I pass the output to a alertbox and this is what i get.
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens' in /Applications/XAMPP/xamppfiles/htdocs/jen/maintabs/submit.php:33
Stack trace:
#0 /Applications/XAMPP/xamppfiles/htdocs/jen/maintabs/submit.php(33): PDOStatement->execute()
#1 {main}
thrown in /Applications/XAMPP/xamppfiles/htdocs/jen/maintabs/submit.php on line 33
Anyone see where i’m going wrong?
UPDATE 1
I added the ‘ in front of :comment, this is the new error message
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`login`.`thrives`, CONSTRAINT `thrives_ibfk_1` FOREIGN KEY (`n_emp`) REFERENCES `employees` (`ID`))' in /Applications/XAMPP/xamppfiles/htdocs/jen/maintabs/submit.php:33
Stack trace:
#0 /Applications/XAMPP/xamppfiles/htdocs/jen/maintabs/submit.php(33): PDOStatement->execute()
#1 {main}
thrown in <b>/Applications/XAMPP/xamppfiles/htdocs/jen/maintabs/submit.php</b> on line <b>33</b><br />
Remove the quotes from your bound parameters. It should read
INSERT into thrives (n_emp, comment, g_emp) VALUES (:n_emp, :comment, :g_emp). The single quotes make it a literal string otherwise.