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

如何使用PDO Statement按transType字段筛选数据库表查询结果

实现思路

通过GET请求传递筛选参数,后端动态拼接SQL查询条件,配合PDO预处理保证安全性,下拉菜单、单选按钮都可以适配该逻辑。

第一步:添加前端筛选控件

你可以选择下拉菜单或者单选按钮,选中后自动提交表单刷新结果:

下拉菜单版本

<!-- 页面顶部筛选区域 -->
<form method="get" action="">
  <label>交易类型筛选:</label>
  <select name="trans_type" onchange="this.form.submit()">
    <option value="">全部</option>
    <option value="Investment" <?php if(isset($_GET['trans_type']) && $_GET['trans_type'] == 'Investment') echo 'selected'; ?>>投资</option>
    <option value="Deposit" <?php if(isset($_GET['trans_type']) && $_GET['trans_type'] == 'Deposit') echo 'selected'; ?>>充值</option>
    <option value="Withdrawal" <?php if(isset($_GET['trans_type']) && $_GET['trans_type'] == 'Withdrawal') echo 'selected'; ?>>提现</option>
  </select>
</form>

单选按钮版本

<!-- 页面顶部筛选区域 -->
<form method="get" action="">
  <label>交易类型筛选:</label>
  <input type="radio" name="trans_type" value="" onchange="this.form.submit()" <?php if(!isset($_GET['trans_type']) || empty($_GET['trans_type'])) echo 'checked'; ?>>全部
  <input type="radio" name="trans_type" value="Investment" onchange="this.form.submit()" <?php if(isset($_GET['trans_type']) && $_GET['trans_type'] == 'Investment') echo 'checked'; ?>>投资
  <input type="radio" name="trans_type" value="Deposit" onchange="this.form.submit()" <?php if(isset($_GET['trans_type']) && $_GET['trans_type'] == 'Deposit') echo 'checked'; ?>>充值
  <input type="radio" name="trans_type" value="Withdrawal" onchange="this.form.submit()" <?php if(isset($_GET['trans_type']) && $_GET['trans_type'] == 'Withdrawal') echo 'checked'; ?>>提现
</form>

第二步:修改后端查询逻辑

调整原有的PDO查询代码,动态添加筛选条件,同时做参数合法性校验避免非法请求:

<?php
$conn = $pdo->open();

try{
    // 基础SQL与参数
    $sql = "SELECT * FROM sales WHERE user_id=:user_id";
    $params = ['user_id' => $user['id']];

    // 处理筛选参数
    if (isset($_GET['trans_type']) && !empty($_GET['trans_type'])) {
        // 校验参数属于允许的枚举值,避免非法传入
        $allowTransTypes = ['Investment', 'Deposit', 'Withdrawal'];
        if (in_array($_GET['trans_type'], $allowTransTypes)) {
            $sql .= " AND transType = :trans_type";
            $params['trans_type'] = $_GET['trans_type'];
        }
    }

    // 拼接排序规则
    $sql .= " ORDER BY sales_date DESC";

    $stmt = $conn->prepare($sql);
    $stmt->execute($params);
    foreach($stmt as $row){
        $stmt2 = $conn->prepare("SELECT * FROM details LEFT JOIN products ON products.id=details.product_id WHERE sales_id=:id");
        $stmt2->execute(['id'=>$row['id']]);
        $total = 0;
        foreach($stmt2 as $row2){
            $subtotal = $row2['price']*$row2['quantity'];
            $total += $subtotal;
        }
        // 原有代码里未写完的echo "<input type=button id=>" 属于无效代码,建议删除
        echo "
            <tr>
                <td class='hidden'></td>
                <td>".date('M d, Y', strtotime($row['sales_date']))."</td>
                <td>".$row['transType']."</td>
                <td>".$row['pay_id']."</td>
                <td>&#8369; ".$row['vAmount']."</td>
                <td>".$row['eStatus']."</td>
            </tr>
        ";
    }

}
catch(PDOException $e){
    echo "There is some problem in connection: " . $e->getMessage();
}

$pdo->close();
?>

可选优化:无刷新筛选

如果不需要页面刷新,可以把tbody渲染部分单独抽成接口,前端监听筛选控件的change事件,通过AJAX携带trans_type参数请求接口,拿到返回的HTML直接替换页面的tbody内容即可。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.30 05:15:00