Search code examples
phpmysqlmysql-error-1064

#1064(42000) MySQL Error in INSERT INTO query PHP MySQL


I'm getting the same error some some people got here. But still my problem was not solved. In my University project for my all modules Im getting the same error.

When I'm running the following query in PHP

$username = $_SESSION['username'];
$query = "SELECT id FROM user WHERE username = '$username'";
$qpost = mysqli_query($connection,$query);
$post_user = mysqli_fetch_assoc($qpost);
$forum_size = strlen($forum);
$dt = time();
$datetime = strftime("%Y-%m-%d %H:%M:%S", $dt);
$enteruser = $post_user['id'];
$Query = "INSERT INTO posts('id','user_id','post','date_added') VALUES(NULL,'$enteruser','$forum','$datetime')";`

I'm getting the following error,

#1064 - 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 ''id','user_id','post','date_added') VALUES (NULL,'3','erye yeyeyeyetyery ery ery '' at line 1

And when I was run it in mysql shell I'm getting the same error. When I run that query in phpmyadmin as following,

INSERT INTO posts('id','user_id','post','date_added') VALUES (NULL,3,'erye yeyeyeyetyery ery ery ','2015-07-24 18:53:48');

I'm getting the same error. Can you please help me to solve this.I checked the query in every way I can adn error remains. I cant go forward withour solving this! Thank you for your help!


Solution

  • You don't need apostrophes on your table headers.

            $username = $_SESSION['username'];
            $query = "SELECT id FROM user WHERE username = '$username'";
            $qpost = mysqli_query($connection,$query);
            $post_user = mysqli_fetch_assoc($qpost);
            $forum_size = strlen($forum);
            $dt = time();
            $datetime = strftime("%Y-%m-%d %H:%M:%S", $dt);
            $enteruser = $post_user['id'];
            $Query = "INSERT INTO `posts` (id, user_id, post, date_added) VALUES (NULL,'$enteruser','$forum','$datetime')";