frank5050 Posted March 10, 2011 Share Posted March 10, 2011 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 Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted March 10, 2011 Share Posted March 10, 2011 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. Quote Link to comment Share on other sites More sharing options...
frank5050 Posted March 10, 2011 Author Share Posted March 10, 2011 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? Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted March 10, 2011 Share Posted March 10, 2011 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).")"); } Quote Link to comment Share on other sites More sharing options...
frank5050 Posted March 10, 2011 Author Share Posted March 10, 2011 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. Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted March 10, 2011 Share Posted March 10, 2011 I think the query syntax is getting hosed. Check for query failure and echo mysql_error() if the query doesn't execute. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.