5

I am using this code so I can update a record in database:

$query = mysql_query("UPDATE article 
                         SET com_count = ". $comments_count 
                       WHERE article_id = .$art_id ");

My question is: How can I use variables in a MySQL UPDATE statement.

3 Answers 3

13

$query = mysql_query("UPDATE article set com_count = $comments_count WHERE article_id = $art_id");

You was messing up the quotes and concats.

You can use inline vars like the previous example or concat them like:

$query = mysql_query("UPDATE article set com_count = " . $comments_count . " WHERE article_id = " . $art_id);

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

2 Comments

believe me i would but ..it's said my reputation should be more than 15 ..and it doesn't ..sorry man
Here you go, now you have 16. Don't thank me, just accept the answer so the question could be marked as solved.
4

You messed up on your " . pattern.

$query = mysql_query("UPDATE article set com_count = ". $comments_count . " WHERE article_id = " . $art_id . ");

Comments

2

Use apostrophes when using variables in a MySQL UPDATE statement:

$query = mysql_query("UPDATE article 
                      SET com_count =  '$comments_count'
                      WHERE article_id = '$art_id'");

Be careful about space and apostrophes.

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.