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


inserting data into mysql using PDO, returns error HY093

>我在编码时眼睛真的很模糊,似乎无法找出导致我使用 PDO 将数据插入 mysql HY093错误的原因

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是自动递增的,我不确定使用 '' 是否是声明它的正确方式。

您在post_id之前忘记了:

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

而不是 :

`$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)";`

您可以使用 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));

如果你的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;
}