debug mysqli query with or die mysqli_error

2019-03-04 08:42发布

问题:

I don't know what is wrong.

$result = $db->query("INSERT INTO post_items(`post_id`,`content`,`date`,`user_id`,`category_id`) 
    VALUES ('".$postid.", '".$content."', '".$date."', '".$user_id."', '".$category_id."')");
if($result) { 
    echo "hey";
}

How can I use mysqli_error function to check the cause of error? The syntax of PHP is just fine I think. I guess it has problem with my database.

回答1:

You have a problem with single quotes. You have a ' just before your $postid, but not one after. This means that the SQL query will be seeing '$postid, ' as your first variable and then being confused about the remained.

Try changing your SQL to read:

$result = $db->query("INSERT INTO post_items(`post_id`,`content`,`date`,`user_id`,`category_id`) 
VALUES ('".$postid."', '".$content."', '".$date."', '".$user_id."', '".$category_id."')");

Hope that helps.