Home > OS >  How to change a value between 2 words with IF() in Mysql?
How to change a value between 2 words with IF() in Mysql?

Time:06-02

I am trying to change a value between on / off in a table using this code:

$sql = "UPDATE example SET IF(status = 'on','off','on') WHERE id = '16'";
$connect->exec($sql);

But it is not working correct and I get this error:

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'IF(statuss = 'on','off','on') WHERE id = '16'' at line 1

CodePudding user response:

You're missing the assignment to the field name.

Try with this:

$sql = "UPDATE example SET status = IF(status = 'on','off','on') WHERE id = '16'";
$connect->exec($sql);
  • Related