How to insert multiple check box values into table

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.
[php]
//select ugroup’s from group table.

$result = “SELECT id,ugroup FROM group”;
$res_result = db::getInstance()->query($result);
[/php]

[code]
Tittle :
Description :

//Display ugroups in textboxes and checkboxes
<?php
while( $line=$res_result->fetch(PDO::FETCH_ASSOC)) {
echo ‘’;
echo’<input type=“text” name=“ugroup” disabled=“disabled” value=" ‘. $line[‘ugroup’] .’" size=“7” "/>’;
echo ’ ';
}
?>

[/code]

db_add_page.php

[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 ‘’;
}else{
header(‘Location:…/add_page.php’);
echo ‘’;

}

}[/php]


post-170830-0-89846200-1412929749.jpg

You might be able to do something like the following?
[php]<?php
$db = Database::getInstance();
$pdo = $db->getConnection();

$insertQuery = array(); // Creates the Query String
$insertData = array(); // Creates the Data VALUES
$n = 0; // Array Index:

$sql = 'INSERT INTO add_services (id, tittle, description, g1, g2, g3, g4, g5, g6, g7, g8) VALUES ';
foreach ($records as $innerArray) {
$insertQuery[] = ‘(:id’ . $n . ‘, :tittle’ . $n . ‘, :description’
. $n . ‘, :g1’ . $n . ‘, :g2’ . $n . ‘, :g3’ . $n . ‘, :g4’ . $n
. ‘, :g5’ . $n . ‘, :g6’ . $n . ‘, :g7’ . $n . ‘, :g8’ . $n . ‘)’;

$insertData['id' . $n] = NULL; // I don't think this is needed, you'll have tod delete ':id' . $n?
$insertData['tittle' . $n] = $tittle;
$insertData['description' . $n] = $description;
$insertData['g1' . $n] = $ugroup[0];
$insertData['g2' . $n] = $ugroup[1];
$insertData['g3' . $n] = $ugroup[2];
$insertData['g4' . $n] = $ugroup[3];
$insertData['g5' . $n] = $ugroup[4];
$insertData['g6' . $n] = $ugroup[5];
$insertData['g7' . $n] = $ugroup[6];
$insertData['g8' . $n] = $ugroup[7];
$n += 1;

}
/** Actual Saving of the Data * */
if (!empty($insertQuery)) {
$sql .= implode(’, ', $insertQuery);
$stmt = $pdo->prepare($sql);
$stmt->execute($insertData);
}

header(“location: index.php”);
exit();[/php]

An it even utilizes prepared statements. :wink:

You should auto_increment your id and have it as the primary index in your database table, that way you wouldn’t need the very first $insertData[‘id’ . $n] = NULL; and make sure you edit $insertQuery[] = ‘(:id’ . $n . ‘, :tittle’ . $n . to this $insertQuery[] = ‘(:tittle’ . $n . :wink:

Sponsor our Newsletter | Privacy Policy | Terms of Service