Jump to content

Error in your SQL syntax - using Object Oriented MYSQL query


V

Recommended Posts

I'm modifying all my old MYSQL code into object oriented but I can't get it to work when inserting into the DB. The code below is for inserting a new post into a posts table.

 

require_once("functions.php");

$connection = dbConnect(); //DB connect function

if ($_REQUEST["submit"]) {  
    
$category = mysql_escape_string(strip_tags($_POST["cat_id"]));
        $title = mysql_escape_string(strip_tags($_POST["post_title"]));
$content = mysql_escape_string(strip_tags($_POST["post_content"],
                                              "<a><i><b><img>"));

	if (!$category || !$title || !$content) {
		echo "Please go back and submit a new post.";
		exit;
	}

    $sql = "INSERT INTO posts
                 (cat_id, post_title, post_date, post_content)
          VALUES ('$category', '$title', NOW(), '$content')";
	  
	        $result = $connection->query($sql) or die(mysqli_error($connection));

		$id = mysql_insert_id($connection);
		header("Location: single_post.php?post=$id");

}

 

 

When I try to submit a new post I get

 

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1

 

but it works when I remove

 

$id = mysql_insert_id($connection);
	header("Location: single_post.php?post=$id");

 

Not sure what I'm doing wrong :-\

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.