How to insert multiple check box values into table

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Tje123
Forum Newbie
Posts: 1
Joined: Sun Oct 12, 2014 11:50 pm

How to insert multiple check box values into table

Post by Tje123 »

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 :?: .

Code: Select all

//select ugroup's from group table.

$result = "SELECT id,ugroup FROM group";
$res_result = db::getInstance()->query($result);

Code: Select all

<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>
[text]
db_add_page.php[/text]

Code: Select all

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



}

}
Attachments
ugroup table
ugroup table
post-170830-0-89846200-1412929749.jpg (22.13 KiB) Viewed 1074 times
add_services table
add_services table
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: How to insert multiple check box values into table

Post by Celauran »

Only selected boxes will appear in the post array, so you can't map $_POST['group'][3] directly to column g2. Given that they each have their own column, you'd do better to set the names of the checkboxes accordingly.
Post Reply