dongqiao1151 2014-04-11 13:06
浏览 32
已采纳

使用bindParam和PDO

I've been scratching my head over this code for a couple of hours.... Doesn't make sense to me why it doesn't work

$isCorrect =($question->correct_answer == $body->answer) ? 1:0;
// the values are all there.......
// echo $body->question . "
"; //335
// echo $body->user . "
";     //51324123
// echo $question->day . "
"; //0
// echo $isCorrect . "
";     //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();

gives this error:

SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens

I'm counting 4 tokens... what am I missing? Obviously I'm doing something wrong.

  • 写回答

4条回答 默认 最新

  • dpq59734 2014-04-11 13:08
    关注

    change :

    $stmt->bindParam(":question_id", $body->question);
    

    to:

    $stmt->bindParam(":question", $body->question);
    

    You have use in query :question but binding with wrong key(:question_id).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?