简体   繁体   English

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

[英]inserting data into mysql using PDO, returns error HY093

im really blurry eyed when coding, and cant seem to find out whats causing HY093 error that i get from inserting data to mysql using PDO 我在编码时真的很模糊,并且似乎无法找出导致我从使用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;
}

i get this error HY093 . 我收到此错误HY093

my id is auto-increment, im not sure if using '' is the correct way of declaring it. 我的id是自动递增的,我不确定是否使用''是声明它的正确方法。

You forgot a : before post_id 您在post_id之前忘记了:

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

Instead of : 代替 :

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

use: 采用:

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

You can use NULL: 您可以使用NULL:

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

OR just leave it: 或直接离开:

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

Then: 然后:

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

if your id is auto-increnmented dont send values from php script 如果您的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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM