0

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.this is my code.it's doesn't work.please help me.

//select ugroup's from group table.
    <?php 
    $result = "SELECT id,ugroup FROM group";
    $res_result = db::getInstance()->query($result);
    ?>

group table 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="ugroup" 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

i want to add only selected check box values to relavant fields.

if(isset($_GET))
    {

$tittle = $_GET['tittle'];
    $description = $_GET['description'];
    $ugroup = $_GET['ugroup'];

$acc_status = "INSERT INTO add_services (id,tittle,description,g1,g2,g3,g4,g5,g6,g7,g8) VALUES(NULL,'".$tittle."','".$description."','".$ugroup."','".$ugroup."','".$ugroup."','".$ugroup."','".$ugroup."','".$ugroup."','".$ugroup."','".$ugroup."')";

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



}

}

add_services table add_services table

Tje
  • 71
  • 1
  • 3
  • 15
  • possible duplicate of [How do I insert multiple checkbox values into a table?](http://stackoverflow.com/questions/20176673/how-do-i-insert-multiple-checkbox-values-into-a-table) – Arun Oct 10 '14 at 05:35

1 Answers1

0

Store them in array than Run your query in while loop

Change this

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

To this

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

//See added [ ] afte ugroup

Than in another file you do this

    $check_boxes = implode("','", $_POST['ugroup']);

$query="INSERT add_services (id,tittle,description,g1,g2,g3,g4,g5,g6,g7,g8)
     VALUES (NULL,'".$tittle."','".$description."','{$check_boxes}')";
arif_suhail_123
  • 2,509
  • 2
  • 12
  • 16
  • how can i insert it to each column? – Tje Oct 10 '14 at 06:14
  • it's insert correct values now.but not in relevant column. i click on checkbox1 ,checkbox2 and submit.it's insert to g1 and g2.when i click on checkbox 4,checkbox6 also it insert to g1,g2 columns. – Tje Oct 10 '14 at 06:48
  • now it's show "Uncaught exception 'PDOException' with message 'SQLSTATE[HY000]" error msg. – Tje Oct 10 '14 at 07:29
  • "Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[21S01]: Insert value list does not match column list: 1136 Column count doesn't match value count at row 1' in C:\wamp\www\member\sys-admin\db_sql\db_add_page.php on line 31" error – Tje Oct 10 '14 at 08:44
  • I have 11 columns (id,tittle,description,g1,g2,g3,g4,g5,g6,g7,g8) thank you. – Tje Oct 10 '14 at 09:10