我已经从ugroups表中显示了复选框值(ugroup字段)。现在我想要做的是,当用户选择多个复选框并提交时,应该插入到表中的relavent feild中。这是我的code.it&# 39; s不起作用。请帮助我。
//select ugroup's from group table.
<?php
$result = "SELECT id,ugroup FROM group";
$res_result = db::getInstance()->query($result);
?>
组表
<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
我想只将选中的复选框值添加到相关字段。
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表
答案 0 :(得分:0)
将它们存储在数组中,而不是在while循环中运行查询
更改此
echo '<input type="checkbox" name="ugroup" value=" '. $line['ugroup'] .'" />';
到此
echo '<input type="checkbox" name="ugroup[]" value=" '. $line['ugroup'] .'" />';
//See added [ ] afte ugroup
比在另一个文件中执行此操作
$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}')";