2011-12-08 16 views
0

PHPの挿入クエリで何らかの問題が発生しましたが、なんらかの理由で失敗します。私は変数をエコーし​​て、彼らは何か同等のことをします。PHP PDO準備済みの挿入物。なぜこれは失敗するのですか?

$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(); 

出力をアラートボックスに渡すと、これが表示されます。

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 

誰かが間違っていますか?私は目の前で「追加

UPDATE 1

:コメント、これは新しいエラーメッセージ

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 /> 

答えて

4

では、あなたのバインドされたパラメータから引用符を削除します。それはINSERT into thrives (n_emp, comment, g_emp) VALUES (:n_emp, :comment, :g_emp)と読みます。一重引用符はそれ以外の場合はリテラル文字列にします。

0

引用符をSQLのプレースホルダに適用しないでください。 SQL文字列として次を試してください:

INSERT into thrives (n_emp, comment, g_emp) VALUES (:n_emp, :comment, :g_emp); 
関連する問題