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条)

报告相同问题?

悬赏问题

  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作