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

如何在PHP中筛选MySQL数据进行打印?附现有代码求助

实现筛选后打印订单的解决方案

没问题,我来帮你搞定这个筛选打印功能,咱们分几步来实现:

1. 添加筛选输入表单

首先,在页面里加一个表单,让用户输入筛选条件(这里以客户名为例,你也可以改成订单ID、日期等其他字段),用户提交后触发筛选逻辑,同时保留打印按钮:

<script type="text/javascript">
function printpage() { 
    window.print()
}
</script>
<HTML>
<BODY>
<!-- 新增筛选表单 -->
<form method="GET" action="">
    <label for="customer">筛选客户:</label>
    <input type="text" id="customer" name="customer" placeholder="输入客户名">
    <button type="submit">筛选</button>
    <button type="button" onclick="printpage()">打印筛选结果</button>
</form>
<br>

2. 修改PHP查询逻辑(处理筛选+避免安全问题)

接下来要调整你的PHP代码,处理用户提交的筛选值,同时必须用预处理语句防止SQL注入,还要修正你代码里混用mysqli和废弃的mysql扩展的问题:

<?php
// 替换成你的实际数据库连接信息
$con = mysqli_connect("localhost", "你的用户名", "你的密码", "你的数据库名");

// 检查连接是否成功
if (mysqli_connect_errno()) {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
    exit;
}

// 构建基础查询语句
$sql = "SELECT * FROM order_items";
$params = [];
$types = "";

// 如果用户提交了筛选条件
if (!empty($_GET['customer'])) {
    $sql .= " WHERE CUSTOMER LIKE ?";
    $params[] = "%" . $_GET['customer'] . "%"; // 模糊匹配,支持输入部分客户名
    $types .= "s"; // 标记参数为字符串类型
}

// 使用预处理语句执行查询(防止SQL注入)
$stmt = mysqli_prepare($con, $sql);
if ($stmt) {
    if (!empty($params)) {
        mysqli_stmt_bind_param($stmt, $types, ...$params);
    }
    mysqli_stmt_execute($stmt);
    $result = mysqli_stmt_get_result($stmt);
} else {
    echo "查询准备失败: " . mysqli_error($con);
    exit;
}

// 输出表格(修正了你拼写错误的字段名)
echo "<table border='1'>
        <tr>
            <th>ANAME</th>
            <th>ID</th>
            <th>NAME</th>
            <th>QUANTITY</th>
            <th>PRICE</th>
            <th>CUSTOMER</th>
            <th>DATETIME</th>
            <th>TOTAL COST</th>
            <th>TOTAL WEIGHT</th>
        </tr>";

// 遍历筛选后的结果
while($row = mysqli_fetch_assoc($result)) {
    // 用htmlspecialchars防止XSS攻击
    echo "<tr>
            <td>" . htmlspecialchars($row['ANAME']) . "</td>
            <td>" . htmlspecialchars($row['ID']) . "</td>
            <td>" . htmlspecialchars($row['NAME']) . "</td>
            <td>" . htmlspecialchars($row['QUANTITY']) . "</td>
            <td>" . htmlspecialchars($row['PRICE']) . "</td>
            <td>" . htmlspecialchars($row['CUSTOMER']) . "</td>
            <td>" . htmlspecialchars($row['DATETIME']) . "</td>
            <td>" . htmlspecialchars($row['TOTAL COST']) . "</td>
            <td>" . htmlspecialchars($row['TOTAL WEIGHT']) . "</td>
          </tr>";
}

echo "</table>";

// 清理资源
mysqli_stmt_close($stmt);
mysqli_close($con);
?>
</BODY>
</HTML>

3. 关键细节说明

  • 数据库连接:一定要把mysqli_connect里的空参数替换成你实际的数据库主机、用户名、密码和数据库名。
  • 筛选规则调整:如果需要精确匹配客户名,把LIKE ?改成= ?,同时去掉%" . $_GET['customer'] . "%"里的百分号即可;如果要按其他字段筛选,比如订单ID,只需要修改表单的输入框和SQL里的WHERE条件字段。
  • 字段名一致性:我修正了你代码里拼写错误的TOATAL COAST和TOTAL WIGHT,要确保和你数据库表中的实际字段名完全一致。
  • 安全防护:用htmlspecialchars()转义输出内容防止XSS攻击,用预处理语句防止SQL注入,这两个都是生产环境必须注意的点。

这样用户输入筛选条件后,页面会显示匹配的订单数据,点击打印按钮就只会打印筛选后的内容啦~

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 03:22:05