0

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!

3 Answers 3

1

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')";
Sign up to request clarification or add additional context in comments.

2 Comments

The code worked perfectly when I removed apostrophes, . (I entered same mysql queries shown in phpmyadmin.) Thank you for the help
@thilanka1989 of course. Thank you for your question.
0

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

2 Comments

when I removed apostrophes of table headers problem is solved and I didnt know about now() its saves my time and some lines of coding. Thank you!
Glad I could help. Than you can accept this as an answer so it may be helpful for user In future.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.