Jump to content

Storing an unchecked checkbox value in mySQL


frank5050

Recommended Posts

Hi

 

I have an array of checkboxes whose values if checked can be updated in mysql. The code I have below accomplishes that just fine.

 

 

On my form I have:

 

print "<form method='post' action='update.php'>\n";

/////// mysQL query here

$myID = $itemRow['myID'];
$chk =  $itemRow['item_shipped'] == 1 ? 'checked' : '';

echo "<input type=checkbox name=cbox[] value='{$itemRow['myID']}' $chk>"; 

echo"</form>"; 

 

 

The above code displays various items with a checkbox next to them. So if that checkbox is checked the code below stores that in mysql.

 

 

On my update.php page I have:

 

 


if(sizeof($_POST['cbox'])) {

//means if at least one check box is selected

foreach($_POST['cbox'] AS $id) { 

mysql_query("UPDATE om SET item_shipped ='1' WHERE myID=$id"); 
} //end foreach

} //end IF 


 

 

 

 

The problem is though i can check a checkbox and store that value '1' in mysql, I have no way of unchecking an already checked checkbox and storing the '0' in mysql.

 

Can anyone help?

Thanks in advance

Several ways.  Here's a common one:

$i = 0;
//loop {
   echo "<input type=checkbox name=cbox[$i] value='1' $chk>";
   echo "<input type=hidden name=id[$i] value='$myID' $chk>";
   $i++;
//}

 

if(isset($_POST['cbox'])) {
   foreach($_POST['id'] as $key => $id) {
      if(isset($_POST['cbox'][$key])) {
         $value = 1;
      } else {
         $value = 0;
      }
      mysql_query("UPDATE om SET item_shipped = $value WHERE myID=$id"); 
   }
}

 

Or maybe better so that you don't execute the queries in the loop:

 

if(isset($_POST['cbox'])) {
   foreach($_POST['id'] as $key => $id) {
      if(isset($_POST['cbox'][$key])) {
         $checked[] = $id;
      } else {
         $unchecked[] = $id;
      }
   }
   mysql_query("UPDATE om SET item_shipped = 1 WHERE myID IN (".implode(',', $checked).")"); 
   mysql_query("UPDATE om SET item_shipped = 0 WHERE myID IN (".implode(',', $unchecked).")"); 
}

 

You should use mysql_real_escape_string() or cast the $id to an int to be safe.

Thank you for a fast response:

 

 

When I copied and pasted :

 

$i = 0;
//loop {
   echo "<input type=checkbox name=cbox[$i] value='1' $chk>";
   echo "<input type=hidden name=id[$i] value='$myID' $chk>";
   $i++;
//}

 

in my form page, it displays checkboxes and their current states fine.

 

 

For the second step when I copied/pasted

 

the remaining 2 pieces of code one by one in the update.php page.

 

 

For code:

 


if(isset($_POST['cbox'])) {
   foreach($_POST['id'] as $key => $id) {
      if(isset($_POST['cbox'][$key])) {
         $value = 1;
      } else {
         $value = 0;
      }
      mysql_query("UPDATE om SET item_shipped = $value WHERE myID=$id"); 
   }
}

 

I get the error:

Warning: implode() [function.implode]: Invalid arguments passed in /home/dndm/public_html/cms/update.php on line 53

 

 

For the second code:

 

if(isset($_POST['cbox'])) {
   foreach($_POST['id'] as $key => $id) {
      if(isset($_POST['cbox'][$key])) {
         $checked[] = $id;
      } else {
         $unchecked[] = $id;
      }
   }
   mysql_query("UPDATE om SET item_shipped = 1 WHERE myID IN (".implode(',', $checked).")"); 
   mysql_query("UPDATE om SET item_shipped = 0 WHERE myID IN (".implode(',', $unchecked).")"); 
}

 

I get the error:

Warning: implode() [function.implode]: Invalid arguments passed in /home/dndmobil/public_html/cms/updateom.php on line 53

 

 

What gives?

 

 

 

 

 

 

The code was illustrative and not tested, but you just want the second code and try this:

 

if(is_array($checked)) {
   mysql_query("UPDATE om SET item_shipped = 1 WHERE myID IN (".implode(',', $checked).")");
}
if(is_array($unchecked)) {
   mysql_query("UPDATE om SET item_shipped = 0 WHERE myID IN (".implode(',', $unchecked).")"); 
}

I altered the second code like this:

 


if(isset($_POST['cbox'])) {
   foreach($_POST['id'] as $key => $id) {
      if(isset($_POST['cbox'][$key])) {
         $checked[] = $id;
      } else {
         $unchecked[] = $id;
      }
   }
if(is_array($checked)) {
   mysql_query("UPDATE om SET item_shipped = 1 WHERE myID IN (".implode(',', $checked).")");
}
if(is_array($unchecked)) {
   mysql_query("UPDATE om SET item_shipped = 0 WHERE myID IN (".implode(',', $unchecked).")"); 
}
}

 

Still no luck.

 

Checkbox value doesn't get recorded.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.