将多个复选框值插入一行中的单独列中

时间:2014-10-13 06:00:05

标签: php mysql pdo

我已经从组表中显示了复选框值(ugroup字段)。现在我想要做的是,当用户选择多个复选框并提交时,它应该插入到一行的relavent列中。现在它是什么?插入复选框值。但不在相关列中。这是我的代码。请帮助我。

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



}

}

我点击复选框2,复选框8并提交。插入g1和g2。当我点击复选框1,复选框3时它也被添加到g1和g2.like下面

add_services table

2 个答案:

答案 0 :(得分:2)

更改行

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

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

是的,使用1

启动数组索引

答案 1 :(得分:1)

通常,当我们在复选框中使用$ _POST值时,那些未经检查的值将不会包含在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)
    )

所以你要做的是创建一个默认值(数组),它将保留默认值。

然后将这些输入组合到默认的输入中,以便作为回报,您将拥有完整的插入结构,而不是锯齿状输入。

所以在你的表格中,做这样的事情:

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

然后在您的处理表格上:

$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);
}