Tje
Tje

Reputation: 71

Insert multiple check box values into seperate columns in one row

I have displayed check box values(ugroup field) from group table.now what i want to do is,when user select multiple check boxes and submit it should be insert into relavent column in one row.now it's insert check boxes values.but not in relevant column .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);
?>

group table

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



}

}

i click on checkbox2,checkbox8 and submit.it's insert to g1 and g2.when i click on checkbox 1, checkbox3 its also added to g1 and g2.like below

add_services table

Upvotes: 1

Views: 1409

Answers (2)

Kevin
Kevin

Reputation: 41893

Normally, when we use $_POST values in checkboxes, those that are not checked will not be included in POST.

[group] => Array
    (
        [G1] => G1 // those the one you did not picked will not be included
        [G3] => G3 // so that means your input is jagged
        [G5] => G5 // you cannot hardcode each index (0 - 7)
        [G7] => G7 // or they will be undefined (the ones that are missing)
    )

So what you do is create a default value (an array) which will hold the defaults.

Then you combine those inputs, to the ones you have in default so that in return you will have a complete structure of insertion, instead of jagged inputs.

So in your form, do something like this:

while($line = $res_result->fetch(PDO::FETCH_ASSOC)) {
    echo '<input type="checkbox" name="group['.$line['ugroup'].']" value=" '. $line['ugroup'] .'" />';
                                    // assign G1, G2, indices
    echo'<input type="text" name="ugroup" disabled="disabled" value=" '. $line['ugroup'] .'" size="7" "/>';
    echo ' ';
}

Then on your processing form:

$default_values = array(); //create a default value
while($line = $res_result->fetch(PDO::FETCH_ASSOC)) {
    $default_values[':' . $line['ugroup']] = '';
}

if(isset($_POST)) { // if submitted
    $ugroup = array();
    $temp = $_POST['group'];
    foreach($temp as $val) {
        $ugroup[':' . $val] = $val;
    }

    $combined_input = array_merge($default_values, $ugroup); // combine them so you have a complete structure

    $sql = 'INSERT INTO add_services (tittle, description,g1,g2,g3,g4,g5,g6,g7,g8) VALUES (:title, :description, :G1, :G2, :G3, :G4, :G5, :G6, :G7, :G8)';

    $acc_status = $db->prepare($sql);
    $insert = array(':title' => $title, ':description' => $description,);
    $insert = array_merge($insert, $combined_input);
    $acc_status->execute($insert);
}

Upvotes: 1

Manisha Patel
Manisha Patel

Reputation: 354

Change line

echo '<input type="checkbox" name="group[]" value=" '. $line['ugroup'] .'" />';

To

echo '<input type="checkbox" name="group['.$line['id'].']" value=" '. $line['ugroup'] .'" />';

And yes start array index using 1

Upvotes: 2

Related Questions