Jump to content

How to insert multiple check box values into table


Recommended Posts

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.
$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
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">



$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)
$rate = db::getInstance()->exec($acc_status); 
echo '<script type="text/javascript">alert("Update Error !");</script>';
echo '<script type="text/javascript">alert("Successfuly Updated User Group !");</script>'; 





Edited by Tje
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.