0

编码时我的眼睛真的很模糊,似乎无法找出导致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

myid是自动递增的,我不确定 using''是否是正确的声明方式。

4

4 回答 4

5

你忘记了:之前post_id

$query = "INSERT INTO `comments` (`id`, `post_id`, `comment`) 
          VALUES (:id, :post_id, :comment)";
                       ^---------------------------here
于 2013-11-07T08:56:47.953 回答
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)";`
于 2013-11-07T08:56:57.987 回答
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));
于 2013-11-07T08:58:43.220 回答
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;
}
于 2013-11-07T09:00:27.753 回答