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