CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   PHP (http://www.codingforums.com/forumdisplay.php?f=6)
-   -   Storing an unchecked checkbox value in mySQL (http://www.codingforums.com/showthread.php?t=221009)

frank5050 03-10-2011 06:36 PM

Storing an unchecked checkbox value in mySQL
 
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:

PHP Code:

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

/////// mysQL query here

$myID $itemRow['myID'];
$chk =  $itemRow['item_shipped'] == '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:


PHP Code:


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

tangoforce 03-10-2011 08:16 PM

Easy. Before running your loop set ALL of the records to 0 and then loop through the selected checkboxes and set those to 1.

The only snag is that if you have records you want to keep set as 1 you'll be stuffed unless they were also selected in the form.

Apart from that you can't do it. Checkboxes are for selecting items not for changing their state. Thats what radio buttons are for.

rajesh007king 07-25-2012 02:08 PM

Can anyone Help in Updating database field valu to 0 when a check box is unchecked and submitted.
i need in php...

any help.

thanks in advance


All times are GMT +1. The time now is 08:19 AM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.