Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Feb 2011
    Posts
    32
    Thanks
    3
    Thanked 0 Times in 0 Posts

    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

  • #2
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,299
    Thanks
    57
    Thanked 525 Times in 512 Posts
    Blog Entries
    5
    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.

  • #3
    New to the CF scene
    Join Date
    Jul 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •