Gustav Posted October 19, 2007 Share Posted October 19, 2007 Hi guys, What im stuck with at the moment is how to update the SQL database with the new information acquired from a set of check boxes. What i´ve done so far is to create the checkboxes and got the information from a table which checkboxes is needed and pre check it if it is already enabled. i have two rows in the database 1. Module_Name (name of a module) 2. is_enabled (is the module enabled 1 or 0). The code i have so far is : <FORM action="modules_checked.php" method="post"> <?PHP $array_row = mysql_query("SELECT * FROM check_modules"); while($row = mysql_fetch_array($array_row)){ $module_name = $row["Module_name"]; $array_enabled = mysql_query("SELECT is_enabled FROM check_modules where Module_name='$module_name'"); $enabled = mysql_fetch_array($array_enabled); $db_box1 = $row["is_enabled"]; if($db_box1 == 1) {$set_checked = "CHECKED";} else{$set_checked = "";} print "<input type=\"checkbox\" NAME=\"module[]\" VALUE=\"$module_name\" $set_checked/> $module_name <br>"; $set_checked = ""; } ?> <br> <input type="submit" name="submit" value="Submit" /> </FORM> This part works flawlessly . However i have no clue on how to manage it when someone checks or unchecks and presses submit i can send that to the SQL. With pre thanks, Gustav Quote Link to comment https://forums.phpfreaks.com/topic/73956-checkbox-and-sql/ Share on other sites More sharing options...
BlueSkyIS Posted October 19, 2007 Share Posted October 19, 2007 i typically store checkbox values in MySQL as 0 or 1, that is field of type TINYINT, UNSIGNED. since checkboxes offer multiple options, i give each checkbox a separate name and i set all checkbox values to 1. i do not group checkboxes using an array; i only do that for radio buttons. (so i wouldn't use multiple checkboxes called "module[]"). to get the value of a checkbox for SQL, i use: $checkval = ($_POST['module'] == 1)?1:0; $sql = "UPDATE some_table SET module = '$checkval' WHERE id = '$someid'"; // etc... Quote Link to comment https://forums.phpfreaks.com/topic/73956-checkbox-and-sql/#findComment-373266 Share on other sites More sharing options...
Gustav Posted October 19, 2007 Author Share Posted October 19, 2007 Thanks for a quick reply In order for this to work would you place it inside the while loop where i make the fields ? or after </FORM> or how does it work when you press submit it runs the code ? Quote Link to comment https://forums.phpfreaks.com/topic/73956-checkbox-and-sql/#findComment-373298 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.