I'm trying to update a database field with a 1 or 0, depending on whether a checkbox is checked or not.
It seems to work when the checkbox is checked but not when empty.
Any ideas how to solve this? Code below. Many Thanks, S.
Here's the code within my form:
$query = "SELECT * FROM istable WHERE assocProp = '".$_POST['id']."'";
$result = mysql_query($query);
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
echo '<li>
<label for="changePP'.$row['id'].'"><input id="changePP'.$row['id'].'" type="checkbox" name="showPP_ids[]" value="'.$row['id'].'"'.($row['showPP']=='1' ? ' checked="checked"':NULL).' /> Display</label>
</li>'. PHP_EOL;
}
And the php process code (updated):
$newQuery=mysql_query("select * from ispdfs where assocProp = '".$_POST['id']."'");
$newResult=mysql_fetch_array($newQuery);
foreach ($newResult as $showPP_ids) {
$val = (int) isset($_POST['showPP_ids']);
mysql_query("UPDATE ispdfs SET showPP = $val WHERE id = ".mysql_real_escape_string($showPP_ids));
}