Jump to content

How to insert multiple check box values into table


Tje

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.
<?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>'; 



}

}

post-170830-0-89846200-1412929749_thumb.jpg

post-170830-0-29212300-1412930179_thumb.jpg

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. Please don't revive it unless you have something important to add.

Join the conversation

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

Guest
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.