Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-10-2011, 06:36 PM   PM User | #1
frank5050
New Coder

 
Join Date: Feb 2011
Posts: 32
Thanks: 3
Thanked 0 Times in 0 Posts
frank5050 is an unknown quantity at this point
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
frank5050 is offline   Reply With Quote
Old 03-10-2011, 08:16 PM   PM User | #2
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,658
Thanks: 45
Thanked 456 Times in 444 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
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.
tangoforce is offline   Reply With Quote
Old 07-25-2012, 02:08 PM   PM User | #3
rajesh007king
New to the CF scene

 
Join Date: Jul 2012
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
rajesh007king is an unknown quantity at this point
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
rajesh007king is offline   Reply With Quote
Reply

Bookmarks

Tags
array, checkbox, checkboxes, mysql, php

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:46 AM.


Advertisement
Log in to turn off these ads.