MySQL
`field1` tinyint(1) NOT NULL default '1',
`field2` tinyint(1) NOT NULL default '1',
`field3` tinyint(1) NOT NULL default '1',
`field4` tinyint(1) NOT NULL default '1',
`field5` tinyint(1) NOT NULL default '1',
HTML
<form method="post">
<input type="hidden" name="blah" value="blah">
<input type="checkbox" name="field1" value="1">
<input type="checkbox" name="field2" value="1">
<input type="checkbox" name="field3" value="1">
<input type="checkbox" name="field4" value="1">
<input type="checkbox" name="field5" value="1">
<button type="submit">Submit</button>
</form>
Php
mysql_query("UPDATE `table` SET `field1` = '$_POST[field1]', .......");
So, I want to do the following:
a) If checked, I want to update the corresponding field with 1
b) If unchecked, I want to update the field with 0
And now, please tell me that I do not need to do this, and that there is a better way to do this:
$field1 = isset($_POST['field1']) ? 1 : 0;
$field2 = isset($_POST['field2']) ? 1 : 0;
$field3 = isset($_POST['field3']) ? 1 : 0;
$field4 = isset($_POST['field4']) ? 1 : 0;
$field5 = isset($_POST['field5']) ? 1 : 0;
mysql_query("UPDATE `table` SET `field1` = '$field1', `field2` = '$field2' ....");
user317005
source
share