gpt4 book ai didi

php - 如何将多个复选框值插入到表中

转载 作者:行者123 更新时间:2023-11-29 23:38:35 25 4
gpt4 key购买 nike

我已经显示了 ugroups 表中的复选框值(ugroup 字段)。现在我想做的是,当用户选择多个复选框并提交时,它应该插入到表中的相关字段中。这是我的代码。它不是不起作用。请帮助我。

//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="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 表 add_services table

最佳答案

将它们存储在数组中,然后在 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}')";

关于php - 如何将多个复选框值插入到表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26292552/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com