我一直在抓我的頭在此代碼爲幾個小時.... 沒有道理給我,爲什麼它不工作使用bindParam與PDO
$isCorrect =($question->correct_answer == $body->answer) ? 1:0;
// the values are all there.......
// echo $body->question . "\n"; //335
// echo $body->user . "\n"; //51324123
// echo $question->day . "\n"; //0
// echo $isCorrect . "\n"; //0
//but still the below part fails.
$db = getConnection();
$sql = "INSERT INTO `answers` (`id`, `question_id`, `user`, `day`, `is_correct`) VALUES (NULL, ':question', ':user', ':day', :is_correct)";
$stmt = $db->prepare($sql);
$stmt->bindParam(":question_id", $body->question);
$stmt->bindParam(":user", $body->user);
$stmt->bindParam(":day", $question->day, PDO::PARAM_INT);
$stmt->bindParam(":is_correct", $isCorrect, PDO::PARAM_INT);
$stmt->execute();
給出了這樣的錯誤:
SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens
我在計算4個標記...我錯過了什麼?顯然我做錯了什麼。
有參數無效的數字,因爲你把你的命名參數在單引號,這是不是這樣做的方式。命名參數':question'不能放在單引號中,比'?' –