简体   繁体   中英

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

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 .

my id is auto-increment, im not sure if using '' is the correct way of declaring it.

You forgot a : before 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:

$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

    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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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