gpt4 book ai didi

php - 从同一表单的 3 个下拉列表中添加数据库中的多个选项

转载 作者:行者123 更新时间:2023-11-29 06:14:12 25 4
gpt4 key购买 nike

我有一张表格 3 <select></select>字段和每个下拉用户可以检查多个选项。我想将它们保存在数据库中,但不知道如何制作。这是下拉菜单

<div class="col-lg-4 col-md-4">
<div class="form-group">
<label class="control-label col-sm-4" for="upload_program">Program:</label>
<div class="col-sm-6">
<?php
$pdo = Database::connect();
$sql="SELECT field_name FROM form_fields WHERE group_id=1";
echo '<select class="form-control" name="upload_program" id="ddlCars2" multiple="multiple">';
foreach ($pdo->query($sql) as $row_program){

echo '<option value='.$row_program['field_name'].'>'.$row_program['field_name'].'</option>';
}
echo '</select>';
?>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-4" for="upload_subject">Subject:</label>
<div class="col-sm-6">
<?php
$pdo = Database::connect();
$sql="SELECT field_name FROM form_fields WHERE group_id=2";
echo '<select class="form-control" name="upload_subject" id="ddlCars1" multiple="multiple">';
foreach ($pdo->query($sql) as $row_subject){
echo '<option value='.$row_subject['field_name'].'>'.$row_subject['field_name'].'</option>';
}
echo '</select>';
?>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-4" for="upload_grade_level">Grade Level:</label>
<div class="col-sm-6">
<?php
$pdo = Database::connect();
$sql="SELECT field_name FROM form_fields WHERE group_id=4";
echo '<select class="form-control" name="upload_grade_level" id="ddlCars3" multiple="multiple">';
foreach ($pdo->query($sql) as $row_grade_level){
echo '<option value='.$row_grade_level['field_name'].'>'.$row_grade_level['field_name'].'</option>';
}
echo '</select>';
?>
</div>
</div>
</div>

这就是我在页面上显示它们的方式。这是插入它们的查询,但不插入所有选中的选项..

$upload_program = $_POST['upload_program'];
$upload_subject = $_POST['upload_subject'];
$upload_grade_level = $_POST['upload_grade_level'];

$stmt1 = $pdo->prepare('INSERT INTO table (upload_program, upload_subject, upload_grade_level) VALUES (:upload_program, :upload_subject, :upload_grade_level)');

$stmt1->execute(array(
':upload_program' => $upload_program,
':upload_subject' => $upload_subject,
':upload_grade_level' => $upload_grade_level
));

我知道我可以以某种方式将它们作为数组但不知道如何......

最佳答案

您需要在选择下拉列表中使用 [] 数组来选择多个选项。

语法:

<select name="test[]" id="dropdown1" multiple="multiple">

在 PHP 中,您可以使用选择名称在 post 数组中获取值,如上例所示,您将在 $_POST['test'] 数组中获取值。

HTML

<div class="col-lg-4 col-md-4">
<div class="form-group">
<label class="control-label col-sm-4" for="upload_program">Program:</label>
<div class="col-sm-6">
<?php
$pdo = Database::connect();
$sql="SELECT field_name FROM form_fields WHERE group_id=1";
echo '<select class="form-control" name="upload_program[]" id="ddlCars2" multiple="multiple">';
foreach ($pdo->query($sql) as $row_program){

echo '<option value='.$row_program['field_name'].'>'.$row_program['field_name'].'</option>';
}
echo '</select>';
?>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-4" for="upload_subject">Subject:</label>
<div class="col-sm-6">
<?php
$pdo = Database::connect();
$sql="SELECT field_name FROM form_fields WHERE group_id=2";
echo '<select class="form-control" name="upload_subject[]" id="ddlCars1" multiple="multiple">';
foreach ($pdo->query($sql) as $row_subject){
echo '<option value='.$row_subject['field_name'].'>'.$row_subject['field_name'].'</option>';
}
echo '</select>';
?>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-4" for="upload_grade_level">Grade Level:</label>
<div class="col-sm-6">
<?php
$pdo = Database::connect();
$sql="SELECT field_name FROM form_fields WHERE group_id=4";
echo '<select class="form-control" name="upload_grade_level[]" id="ddlCars3" multiple="multiple">';
foreach ($pdo->query($sql) as $row_grade_level){
echo '<option value='.$row_grade_level['field_name'].'>'.$row_grade_level['field_name'].'</option>';
}
echo '</select>';
?>
</div>
</div>
</div>

PHP

使用 implode() 您可以将您选择的选项从数组更改为字符串,以便将其存储在数据库表中。

$upload_program = implode(",",$_POST['upload_program']); // You will get value with comma seperated
$upload_subject = implode(",",$_POST['upload_subject']);
$upload_grade_level = implode(",",$_POST['upload_grade_level']);

$stmt1 = $pdo->prepare('INSERT INTO table (upload_program, upload_subject, upload_grade_level) VALUES (:upload_program, :upload_subject, :upload_grade_level)');

$stmt1->execute(array(
':upload_program' => $upload_program,
':upload_subject' => $upload_subject,
':upload_grade_level' => $upload_grade_level
));

关于php - 从同一表单的 3 个下拉列表中添加数据库中的多个选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36789757/

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