Page 1 of 1

How to insert multiple check box values into table

Posted: Sun Oct 12, 2014 11:54 pm
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>'; 



}

}

Re: How to insert multiple check box values into table

Posted: Mon Oct 13, 2014 10:45 am
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.