0

This sql query is not working:

$sql = "INSERT INTO top(topic_subject,topic_date, topic_cat, topic_by)
VALUES(" . mysql_real_escape_string($_POST['topic_subject']) . " , NOW()," . mysql_real_escape_string($_POST['topic_cat']) . " , " . isset ($_SESSION['user_id']) . ")";

how can I fix it?. I am getting this error message.

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 2`
1
  • 6
    1. You're not quoting your strings. 2. Parameterize your queries. Commented Mar 24, 2014 at 21:25

3 Answers 3

2

It's likely that topic_subject is character data. To include literal strings in SQL text, it should be enclosed in single quotes.

... VALUES ('abc', ...

If you used prepared statements, this wouldn't be an issue, and for the love of all things that are beautiful and good in this world, don't use the deprecated PHP mysql_ interface for new development. It's been superseded by the mysqli_ and PDO interfaces.

Sign up to request clarification or add additional context in comments.

Comments

1

You forgot the quotes.

$sql = "INSERT INTO top(topic_subject,topic_date, topic_cat, topic_by)
VALUES('" . mysql_real_escape_string($_POST['topic_subject']) . "' , NOW(),'" . mysql_real_escape_string($_POST['topic_cat']) . "' , '" . isset ($_SESSION['user_id']) . "')";

And be aware that mysql_* is deprecated. Use PDO or mysqli instead.

Comments

0

There are couple problems here.

  1. Quote your strings
  2. Make sure your data is of the correct type

    $topic_subject = mysql_real_escape_string($_POST['topic_subject']);
    $topic_date = NOW();
    $topic_cat = mysql_real_escape_string($_POST['topic_cat']);
    $topic_by = isset($_SESSION['user_id']) ? $_SESSION['user_id'] : ""; // always returns a string value.

    $sql = "INSERT INTO top(topic_subject,topic_date, topic_cat, topic_by) 
    VALUES('{$topic_subject}' , {$right_now}, '{$topic_cat}' , '{$topic_by}')";  

It may help you to use more variables in your code (shown) so that you can use a debugger to verify that the strings and variables you create have the values you intend them to have.

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.