-1

I am trying to update some values in a table via MySQL and php, but I cannot it to work: Here is my code:

$update = "UPDATE $table 
           SET `active`='1' AND `title`='Disabled By Moderator' 
           WHERE `id`='".$ad_id."' LIMIT 1";
Josh Mein
  • 28,107
  • 15
  • 76
  • 87
Tahoe Cale
  • 83
  • 1
  • 2
  • 8
  • Before asking Question to **SO** you should have tried studying **update syntax.** and you would have solved it in **less than 30 secs.** – Prahalad Gaggar Apr 26 '13 at 17:52

1 Answers1

3

when updating multiple columns, separated it with a comma.

$update = "UPDATE $table SET `active`='1' , `title`='Disabled By Moderator' WHERE `id`='".$ad_id."' LIMIT 1";

the reason why it is updating 1 or 0 on column active is because the statement is performing boolean operation. The only column that is affected on your current statement is only column Active. Column title is part of the boolean operation.

Your update statement will look like if it is grouped,

UPDATE $table 
SET    active = (1 AND (title = 'Disabled By Moderator'))
WHERE  id= 'val' 
LIMIT  1

As a sidenote, the query is vulnerable with SQL Injection if the value(s) of the variables came from the outside. Please take a look at the article below to learn how to prevent from it. By using PreparedStatements you can get rid of using single quotes around values.

Community
  • 1
  • 1
John Woo
  • 258,903
  • 69
  • 498
  • 492