Reputation: 2683
I have a Mysqli table called status with the columns ID, STATUS.
The STATUS can be 1 or 2 only.
I want to update the STATUS column the easiest way, so if the STATUS value is 1, update it to 2 and vice versa.
Normally I can do it like:
$c=$mysqli->query('SELECT * FROM status WHERE id="1"' );
$kats=$c->fetch_assoc();
$status=$kats['status'];
if ($status == '1') {$newstatus = '2';} else {$newstatus = '1';}
$mysqli->query("UPDATE status SET status = '$newstatus' WHERE id=1" );
This will work, but is there an easier way, or maybe 1 row query for this? As the values are just changing from 1 to 2 or from 2 to 1 in one column.
Upvotes: 0
Views: 251
Reputation: 355
If you only have two values then consider using a Boolean data type. If you do then you can do it in one query, something like this:
UPDATE status SET STATUS = !STATUS WHERE ID = ?
Also, it's kinda confusing having status
as both a table and column name.
Upvotes: 1