Simple mysql toggle (tinyint value)
Saturday, October 16th, 2010 | Author: Matjaž Hladnik
A very simple way to toggle a tinyint field in your mysql table.
The mysql table example:
CREATE TABLE `products` ( `id` int(10) NOT NULL auto_increment, `name` varchar(50) NOT NULL, `visible` tinyint(1) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
$sql = mysql_query(" UPDATE products SET visible = visible <> 1 WHERE id = '$id' ");
If you want to toggle the visible field of a specific product, just execute this query. If the field visible has value 0, it will be updated to 1. If it is already 1, it will be updated to 0.
Extra tip: I put the $id variable inside the ‘…’ quotes to avoid broken query, if the $id is empty or not an integer.
Category: Web development
| | One Comment

