DarkSynopsis Posted October 30, 2012 Share Posted October 30, 2012 Quite new to PHP and right now I'm trying to have it so when a Checkbox is checked row "cb_value" becomes 1 depending on what item the Checkbox is next to based on ID and if not checked the "cb_value" would be 0 so users can freely update a list. I'm trying to create a Simple Test page for this with a Test Database and learn from there how to use it where I want to. Database: cd_id | cb_value 1 | 1 2 | 1 3 | 0 4 | 0 Right now my code is as follows <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Testing Checkbox Submission</title> </head> <?php include "config.php"; ?> <body> <table width="90%" border="0" align="center" cellpadding="3" cellspacing="1" bgcolor="white"> <form method="post"> <tr bgcolor="#CCCCCC" align="center"> <td width="36%"><h4>Checkbox</h4></td> </tr> <?php $query = "SELECT * FROM test"; $result = mysql_query($query); while($row = mysql_fetch_assoc($result)) { // Start looping table row $id = $row['cb_id']; $checkbox = $row['cb_value']; if (isset($_POST['submit'])) { $select = mysql_query("SELECT * FROM test"); $row = mysql_fetch_assoc($select); $id = $row['cb_id']; $checkbox = $_POST['checkbox']; $query = mysql_query("UPDATE test SET cb_value = '$checkbox' WHERE cb_id = '$id'"); } ?> <tr align="center" bgcolor="#eeeeee"> <td> <input type="checkbox" name="checkbox" value="1" <?php if($checkbox == '1') { echo "checked='checked'"; } ?>/> </td> </tr> <?php } // end of while loop ?> <tr> <td> <input type="submit" name="submit" value="Update"/> </td> </tr> </form> </table> </body> </html> I can't seem to wrap my head around this I've had no problems in the past updating a field in a Database via PHP but come to think about it there is normally only 1 ID involved this needs to be able to grab each one and then give it a value of either 0 or 1 depending on the state of the Checkbox. Last time I ran the above code no matter what Checkbox I tick ID 1 ends up getting 1 in the "cb_value" and unchecking and update does not give 0 but I can't imagine it would without some form of code to say do that which I have no idea how to do right now. Hopefully someone can help me out and explain where I'm going wrong and such. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/270088-update-multiple-checkboxes-where-id/ Share on other sites More sharing options...
Jessica Posted October 30, 2012 Share Posted October 30, 2012 The checkbox name needs to be an array name="values[]" Quote Link to comment https://forums.phpfreaks.com/topic/270088-update-multiple-checkboxes-where-id/#findComment-1388840 Share on other sites More sharing options...
Barand Posted October 30, 2012 Share Posted October 30, 2012 something like this <?php include("testDBconnect.php"); /******************** * Process form data ********************/ if (isset($_POST['cb_value'])) { mysql_query("UPDATE test SET cb_value = 0"); // unset all values; $tmp = array_map('intval', $_POST['cb_value']); // cleanse input $idlist = join(',', $tmp); // create a comma-separated list of the ids /************************************** * only checked checkboxes are posted * so set values of the posted ids to 1 ***************************************/ mysql_query("UPDATE test SET cb_value = 1 WHERE cd_id IN ($idlist)"); } /********************** * display the form ***********************/ echo "<form method='post' >"; $sql = "SELECT cd_id, cb_value FROM test"; $res = mysql_query($sql); while (list($id, $val) = mysql_fetch_row($res)) { $checked = $val==1 ? 'checked':''; echo "<input type='checkbox' name='cb_value[]' value='$id' $checked /> $id<br />"; } echo "<input type='submit' name='btnSubmit' value='Submit'/> </form>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/270088-update-multiple-checkboxes-where-id/#findComment-1388868 Share on other sites More sharing options...
nodirtyrockstar Posted October 31, 2012 Share Posted October 31, 2012 (edited) First, Jessica is right. Second, if a user selects a checkbox field, and then submits it to PHP, it will be set to 'on' in the post array. You don't have to create an additional variable to reflect that it has been set, because PHP provides it for you. Thus, here is an example using your code: [code<input type="submit" name="submit[]" value="Update"/>[/code] Note the square brackets after "submit" in the name field. This is what will give you an array of checkbox values. Then you can reference their values like so: if (!empty($_POST['submit'])) //though I would recommend changing this variable name, I have left it as you originally marked it up { foreach($_POST['submit'] as $key => $value) { if ($_POST['submit'][$key] === 'on') { Now you can set new variables based on what you find within your post array. } } } Edited October 31, 2012 by nodirtyrockstar Quote Link to comment https://forums.phpfreaks.com/topic/270088-update-multiple-checkboxes-where-id/#findComment-1388878 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.