Tje Posted October 10, 2014 Share Posted October 10, 2014 (edited) I have displayed check box values(ugroup field) from ugroups table.now what i want to do is,when user select multiple check boxes and submit it should be insert into relavent feild in table.now it's insert check boxes values.but not in relevant field.this is my code.Please help me. //select ugroup's from group table. <?php $result = "SELECT id,ugroup FROM group"; $res_result = db::getInstance()->query($result); ?> <form action="db_sql/db_add_page.php" method="get"> Tittle :<input type="text" size="100" name="tittle" /> Description :<textarea cols="80" id="editor1" name="description" rows="10"></textarea> //Display ugroups in textboxes and checkboxes <?php while( $line=$res_result->fetch(PDO::FETCH_ASSOC)) { echo '<input type="checkbox" name="group[]" value=" '. $line['ugroup'] .'" />'; echo'<input type="text" name="ugroup" disabled="disabled" value=" '. $line['ugroup'] .'" size="7" "/>'; echo ' '; } ?><input type="submit" value="Submit"> </form> db_add_page.php if(isset($_POST)) { $tittle = $_POST['tittle']; $description = $_POST['description']; $ugroup = $_POST['group']; $acc_status = "INSERT INTO add_services (id,tittle,description,g1,g2,g3,g4,g5,g6,g7,g8) VALUES(NULL,'".$tittle."','".$description."','".$ugroup[0]."','".$ugroup[1]."','".$ugroup[2]."',' ".$ugroup[3]."','".$ugroup[4]."','".$ugroup[5]."','".$ugroup[6]."','".$ugroup[7]."')"; $rate = db::getInstance()->exec($acc_status); if(!$rate){ echo '<script type="text/javascript">alert("Update Error !");</script>'; }else{ header('Location:../add_page.php'); echo '<script type="text/javascript">alert("Successfuly Updated User Group !");</script>'; } } Edited October 10, 2014 by Tje Quote Link to comment Share on other sites More sharing options...
Frank_b Posted October 10, 2014 Share Posted October 10, 2014 The $_POST['checkbox'] value will only exist if the checkbox is checked. Thats why you have to reset the corresponding database field to NULL or 0 by default. And then you have to test if the $_POST['checkbox'] exist with the isset() function. If it does then you set the database field to 1. Quote Link to comment Share on other sites More sharing options...
Tje Posted October 10, 2014 Author Share Posted October 10, 2014 i checked my code with isset().but same happen.i click on checkbox1,checkbox2 and submit.it's insert to g1 and g2.when i click on checkbox 4, checkbox8 its also added to g1 and g2.like add_service.jpg Quote Link to comment Share on other sites More sharing options...
Barand Posted October 10, 2014 Share Posted October 10, 2014 The correct way to do it is to normalize your data and remove the groups g1, g2,..., gN from the services table and to create a new table, say, service_group. The group ids should each be stored in a separate row with the service_id +-------------------+ +-------------+ | add_service | | ugroup | +-------------------+ +-------------+ | id | ---+ +------ | id | | title | | +-----------------+ | | ugroup | | description | | | service_group | | +-------------+ +-------------------+ | +-----------------+ | +----< | service_id | | | ugroup_id | >---+ +-----------------+ Then for each checkbox that was checked you write a record to this third table 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.