You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP执行双表SQL查询生成HTML表单下拉列表空白问题求助

问题分析与修复方案

核心问题

你的代码存在多处变量引用和字段匹配错误,导致下拉列表能生成对应数量的空选项,具体问题点:

  • 第一个下拉列表:mysqli_fetch_array传入了错误的变量$inputA(应为查询结果$mc_list),同时尝试读取不存在的字段inputA(实际查询的是fruit字段)
  • 第二个下拉列表:查询结果覆盖了表单变量$inputB,且尝试读取不存在的字段inputB(实际查询的是veggies字段),下拉框name属性与表单提交字段不匹配(应为inputB而非responding_mbr)
  • 数据库连接文件被重复包含在下拉代码块中,可能引发连接冲突

修复后的完整代码

<!-- 添加新条目到数据库-->
<?php
// 提前引入数据库连接,避免重复包含
include_once("config.php");

// 初始化表单变量
$inputA = "";
$inputB = "";

$errorMessage = "";
$successMessage = "";

// 处理POST提交请求
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $inputA = mysqli_real_escape_string($connection, $_POST['inputA']);
    $inputB = mysqli_real_escape_string($connection, $_POST['inputB']);
    
    do {
        // 必填字段验证
        if (empty($inputA) || empty($inputB)) {
            $errorMessage = "所有字段为必填项";
            break;
        } 
        
        // 使用预处理语句避免SQL注入,提升安全性
        $sql = "INSERT INTO trial (inputA, inputB) VALUES (?, ?)";
        $stmt = $connection->prepare($sql);
        $stmt->bind_param("ss", $inputA, $inputB);
        $result = $stmt->execute();

        if (!$result) {
            $errorMessage = "无效查询: " . $connection->error;
            break;
        }

        // 重置变量
        $inputA = "";
        $inputB = "";
        $successMessage = "条目添加成功";
        
        // 跳转回列表页
        header("location: index.php");
        exit;

    } while (false);
}
?>

<!-- HTML页面结构-->
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>添加新条目</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css">
</head>

<body>
    <div class="container my-5">
        <h2>新条目</h2>

        <!-- 错误提示 -->
        <?php
        if (!empty($errorMessage)){
            echo"
            <div class='alert alert-warning alert-dismissible fade show' role='alert'>
                <strong>$errorMessage</strong>
                <button type='button' class='btn-close' data-bs-dismiss='alert' aria-label='Close'></button>
            </div>
            ";
        }
        ?>
        
        <form method="post">
  
            <!-- 第一个下拉列表:从fruit表获取数据 -->
            <div class="row mb-3">
                <label class="col-sm-3 col-form-label">Input A</label>
                <div class="col-sm-6">
                    <select name="inputA" class="form-control">
                    <option value="" disabled="disabled" selected="selected">选择Input A</option>
                    <?php
                        $sql = "SELECT fruit FROM fruit";
                        $mc_list = $connection->query($sql);
                        while ($category1 = mysqli_fetch_array($mc_list, MYSQLI_ASSOC)):
                    ?>
                        <option value="<?php echo htmlspecialchars($category1["fruit"]); ?>">
                            <?php echo htmlspecialchars($category1["fruit"]); ?>
                        </option>
                    <?php
                        endwhile;
                    ?>
                    </select>
                </div>
            </div>

            <!-- 第二个下拉列表:从veggies表获取数据 -->
            <div class="row mb-3">
                <label class="col-sm-3 col-form-label">Input B</label>
                <div class="col-sm-6">
                    <select name="inputB" class="form-control">
                    <option value="" disabled="disabled" selected="selected">选择Input B</option>
                    <?php
                        $sql1 = "SELECT veggies FROM veggies";
                        $veggies_list = $connection->query($sql1); // 用独立变量存储查询结果,避免覆盖表单变量
                        while ($category2 = mysqli_fetch_array($veggies_list, MYSQLI_ASSOC)):
                    ?>
                        <option value="<?php echo htmlspecialchars($category2["veggies"]); ?>">
                            <?php echo htmlspecialchars($category2["veggies"]); ?>
                        </option>
                    <?php
                        endwhile;
                    ?>
                    </select>
                </div>
            </div>

            <!-- 成功提示 -->
            <?php
            if (!empty($successMessage)) {
                echo "
                <div class='row mb-3'>
                    <div class='offset-sm-3 col-sm-6'>
                        <div class='alert alert-success alert-dismissible fade show' role='alert'>
                            <strong>$successMessage</strong>
                            <button type='button' class='btn-close' data-bs-dismiss='alert' aria-label='Close'></button>
                        </div>
                    </div>
                </div>
                ";
            }
            ?>

            <!-- 操作按钮 -->
            <div class="row mb-3">
                <div class="offset-sm-3 col-sm-3 d-grid">
                    <button type="submit" class="btn btn-primary">提交</button>
                </div>
                <div class="col-sm-3 d-grid">
                    <a class="btn btn-outline-primary" href="index.php" role="button">取消</a>
                </div>
            </div>
        </form>
    </div>

<?php
    // 关闭数据库连接
    mysqli_close($connection);
?>

</body>
</html>

额外优化说明

  1. 将数据库连接移至PHP代码开头,避免重复包含引发的连接问题
  2. 使用htmlspecialchars()转义输出内容,防止XSS攻击
  3. 替换为预处理语句执行INSERT操作,避免SQL注入风险
  4. 修正了HTML中的拼写错误(如land改为lang,width-device-width改为width=device-width,colsm-3改为col-sm-3)

内容的提问来源于stack exchange,提问作者user21397714

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 21:43:09