PHP PDO/MySQL : Dropdown values from database table cannot be inserted to another table

标签 php mysql select insert

我正在制作一个服务台票务系统。从下拉列表中插入数据(从数据库表列中选择值)到数据库表似乎有问题。我将 PHP PDO 与 MySQL 结合使用。

我的选择下拉菜单如下所示:

Dropdown picture

<div class="form-group">
    <label class="control-label col-md-3 col-sm-3 col-xs-12">Department <span class="required">*</span></label>
    <div class="col-md-9 col-sm-9 col-xs-12">
        <select class="form-control" name="department" required>
            <option value="" disabled selected></option>
            <?php
                $department_query = "SELECT * FROM department";
                $stmt = $db->prepare($department_query);
                $stmt->execute();
                while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
                echo "<option value='".$row['$department']."''>".$row['department']."</option>";
                }
            ?>
        </select>
    </div>
</div>

它已经可以在下拉选项中读取和显示数据 department_name

为了从我的表单中插入值,它已经可以插入除下拉列表中的值之外的其他值。

我用于插入的内联 php 代码是:

<?php 
          if(isset($_POST['addticket'])){
            $stmt = $db->prepare("INSERT INTO ticket (requestor_name, employee_name, department_concern, subject, description, priority) VALUES (:Requestor, :Employee, :Department, :Subject, :Description, :Priority)");
            $stmt->bindParam(':Requestor', $Requestor);
            $stmt->bindParam(':Employee', $Employee);
            $stmt->bindParam(':Department', $Department);
            $stmt->bindParam(':Subject', $Subject);
            $stmt->bindParam(':Description', $Description);
            $stmt->bindParam(':Priority', $Priority);

            $Requestor = $_POST['requestor'];
            $Employee = $_POST['employee'];
            $Department = $_POST['department'];
            $Subject = $_POST['subject'];
            $Description = $_POST['description'];
            $Priority = $_POST['priority'];
            $stmt->execute();

          }
        ?>

没有任何错误,但是选择下拉列表的值没有插入到我的数据库中,而插入了其他值。见下图。

Database Result picture

我已尝试更改 name=""$_POST[''] 数百次 - 结果仍然相同,但我找不到答案。

我只想显示

"<option value='".$row['$department']."''>".$row['department']."</option>"

来自

<select class="form-control" name="department" required>

$Department = $_POST['department'];

以便它可以插入到我的数据库中。

如果有人能帮助我就太好了。请。 提前谢谢你:)

最佳答案

<option value="" disabled selected></option>

从您的选项中删除禁用

关于PHP PDO/MySQL : Dropdown values from database table cannot be inserted to another table,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43221530/

相关文章:

php - CASE 内的子查询的别名

PHP:我得到一个完全空白的页面,我不知道如何在 PHP 中调试它

php - 使用哪个,concat,concat_ws?在 mysql 中

php - 如何使用 Facebook 作为我网站的网关

sql - 对选定项的操作

javascript - 跟踪哪个列表元素被点击并将此数据传输到 PHP

php - 使用过滤器根据类别进行搜索

PHP使用regex循环查询mysql输出一个excel文件

MySQL 查询 : select all the books a user has completed reviewing

javascript - 如何使用 Jquery 检查 URL 参数值是否存在?