thilanka1989
thilanka1989

Reputation: 77

#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!

Upvotes: 0

Views: 848

Answers (3)

Sachitha Dilshan
Sachitha Dilshan

Reputation: 343

Try this...

$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 ('user_id','post','date_added') VALUES('$enteruser','$forum','$datetime')";`

Upvotes: 0

pinkal vansia
pinkal vansia

Reputation: 10330

Given id is primary-key and auto-increment, you can exclude it. You can also use native Mysql function now() for date_added

$Query = "INSERT INTO `posts` (user_id, post, date_added) VALUES ('$enteruser','$forum',now())";

Upvotes: 0

GrumpyCrouton
GrumpyCrouton

Reputation: 8620

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')";

Upvotes: 1

Related Questions