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

请求协助修复PHP代码:下拉选建筑后显示对应地点访问次数

问题修复:仅显示选中建筑对应的访问次数

你的代码目前会展示所有地点的统计结果,主要是两个核心问题导致的,我来帮你一步步修复:

核心问题分析

  1. 下拉选中状态判断错误:你把提交的building_ID和building_name做比较,这两个值的类型和内容完全不匹配,导致选中的选项无法正确高亮。
  2. 统计查询未过滤选中建筑:当前的COUNT查询没有添加任何过滤条件,会返回所有location_ID的统计结果,而不是仅选中的那一个。

另外,直接把用户输入的$_POST['bldg']拼进SQL语句存在SQL注入风险,我会帮你改成参数化查询来避免这个安全问题。

修复后的完整代码

<?php
$con = new mysqli("localhost","root","","user_databases");
// 检查数据库连接是否成功
if ($con->connect_error) {
    die("数据库连接失败: " . $con->connect_error);
}

// 查询建筑列表用于下拉框
$bquery = mysqli_query($con, "SELECT building_ID, building_name FROM buildings");
$selectedbldg = null;
$countResult = null;

// 处理表单提交
if (!empty($_POST['bldg'])) {
    // 存储选中的建筑ID
    $selectedbldg = $_POST['bldg'];
    
    // 使用参数化查询统计选中建筑的访问次数
    $stmt = $con->prepare("SELECT location_ID, COUNT(location_ID) AS visit_count 
                           FROM delivery_transaction 
                           WHERE location_ID = ? 
                           GROUP BY location_ID");
    $stmt->bind_param("i", $selectedbldg); // "i"表示整数类型,若你的字段是字符串可改为"s"
    $stmt->execute();
    $countResult = $stmt->get_result();
    $stmt->close();
}
?>

<!--建筑下拉框表单-->
<form name="bldg_form" method="post" action="">
    <select name="bldg">
        <option value="">Choose Building</option>
        <?php while ($row = mysqli_fetch_assoc($bquery)) : ?>
            <option value="<?= $row['building_ID'] ?>" <?= $row['building_ID'] == $selectedbldg ? 'selected' : '' ?>>
                <?= $row['building_name'] ?>
            </option>
        <?php endwhile ?>
    </select>
    <input type="submit" name="view" />
</form>

<section class="row text-center placeholders">
    <!--统计结果展示表格-->
    <div class="table-responsive">
        <table class="table table-striped">
            <thead>
                <tr>
                    <th>Location</th>
                    <th>Number of Visits</th>
                </tr>
            </thead>
            <tbody>
                <?php if (isset($countResult) && mysqli_num_rows($countResult)) : ?>
                    <?php while($row = mysqli_fetch_assoc($countResult)) : ?>
                        <tr>
                            <td><?= $row['location_ID'] ?></td>
                            <td><?= $row['visit_count'] ?></td>
                        </tr>
                    <?php endwhile ?>
                <?php else : ?>
                    <tr>
                        <td colspan="2">No results to display</td>
                    </tr>
                <?php endif ?>
            </tbody>
        </table>
    </div>
</section>

关键修改说明

  1. 修正下拉选中判断:把$row['building_name'] == $selectedbldg改成$row['building_ID'] == $selectedbldg,确保选中的选项能正确高亮显示。
  2. 添加WHERE过滤条件:在统计查询里加入WHERE location_ID = ?,只查询选中建筑对应的地点访问次数。
  3. 参数化查询优化:使用mysqli的预处理语句,避免SQL注入攻击,同时保证查询的安全性和稳定性。
  4. 字段别名优化:给COUNT(location_ID)起了别名visit_count,让代码更易读,避免直接使用$row['COUNT(location_ID)']这种不友好的写法。
  5. 空结果布局优化:把单单元格的提示改成跨两列的<td colspan="2">,避免表格布局错乱。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 04:11:51