在同一表单上的3个下拉列表中添加数据库中的多个选项

时间:2016-04-22 09:17:01

标签: php mysql

我在一个表单上有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            
        ));

我知道不知怎的,我可以将它们作为数组但不知道如何......

2 个答案:

答案 0 :(得分:1)

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

语法:

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

答案 1 :(得分:1)

对于获取选择的多个选项,您的选择名称应该是数组,并且您将在post中使用select_name数组来获取它,您可以将其内爆