使用PDO将数据插入mysql,返回错误HY093

时间:2013-11-07 08:54:59

标签: php mysql pdo

在编码时,我真的很模糊,似乎无法找出导致HY093错误的问题,这是我使用PDO将数据插入mysql时得到的错误

function whatever($post_id, $comment) {
...
$query = "INSERT INTO `comments` (`id`, `post_id`, `comment`) VALUES (:id, post_id,:comment)";
$sql = $db->prepare($query);
$check = $sql->execute(array(':id'=>'',
                             ':post_id'=>$post_id,
                             ':comment'=>$comment));

//verify if data is inserted
if($check) {
    $test = 'inserted';
} else {
    $test = $sql->errorCode();
}
    return $test;
}

我收到此错误HY093

我的id是自动递增的,我不确定使用''是否是正确的声明方式。

4 个答案:

答案 0 :(得分:5)

您在:

之前忘记了post_id
$query = "INSERT INTO `comments` (`id`, `post_id`, `comment`) 
          VALUES (:id, :post_id, :comment)";
                       ^---------------------------here

答案 1 :(得分:1)

而不是:

`$query = "INSERT INTO `comments` (`id`, `post_id`, `comment`) VALUES (:id, post_id,:comment)";`

使用:

`$query = "INSERT INTO `comments` (`id`, `post_id`, `comment`) VALUES (:id, :post_id,:comment)";`

答案 2 :(得分:0)

您可以使用NULL:

$query = "INSERT INTO `comments` (`id`, `post_id`, `comment`) VALUES (NULL, :post_id,:comment)";

或者只是离开它:

$query = "INSERT INTO `comments` (`post_id`, `comment`) VALUES (:post_id,:comment)";

然后:

$check = $sql->execute(array(':post_id'=>$post_id,
                             ':comment'=>$comment));

答案 3 :(得分:0)

如果你的id是自动递增的,不要从php脚本发送值

    function whatever($post_id, $comment) {

     $query = "INSERT INTO `comments` ( `post_id`, `comment`)    VALUES(:id,post_id,:comment)";
       $sql = $db->prepare($query);
   $check = $sql->execute(array(':post_id'=>$post_id,':comment'=>$comment));

  //verify if data is inserted
  if($check) {
  $test = 'inserted';
  } else {
   $test = $sql->errorCode();
 }
  return $test;
}