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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

 

 

 

 

 

 

Link to comment
Share on other sites

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).")"); 
}

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.