Using variables in MySQL UPDATE (PHP/MySQL)

74,726

Solution 1

$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);

Solution 2

You messed up on your " . pattern.

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

Solution 3

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.

Share:
74,726

Related videos on Youtube

MU_FAM
Author by

MU_FAM

Updated on October 28, 2020

Comments

  • MU_FAM
    MU_FAM over 3 years

    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.

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