php 查询的时候 点击下一页 post过来的值为空/丢失 解决方法

本文介绍了一个在PHP分页查询中遇到的问题:点击下一页时原本POST过来的值丢失。通过将搜索值存储在SESSION中并在另一个页面获取该值的方法成功解决了此问题。
2010年02月01日 星期一 22:57

今天遇到一个非常纳闷的问题,就是做在php分页的时候,点击下一页的时候,原本post过来的值确丢失了,我是做的模糊查询,这样一来点了下一页就把所有的信息都给查询出来了,

为了这个问题我把自己的代码研究了好几个小时,始终想不明白为什么会这样,最后用了投机取巧的办法给解决了

最终解决的方法就是在$_POST['searchValue'];到值之后把值放到SESSION里面,然后再另外一个页面利用session取值就可以成功解决了,

posted on 2011-09-05 05:00  之乎者也2011 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/wrmfw/archive/2011/09/05/2166915.html

<?php // 配置数据库连接 $host = 'localhost'; $dbname = 'syxywl.cn_sdk-14'; // 含特殊字符需用反引号包裹 $username = 'root'; $password = 'qq28009618'; // === 跨库查询:获取每个 uid 对应的 name 字段 === try { $uids = array_column($rows, 'uid'); if (!empty($uids)) { $placeholders = str_repeat('?,', count($uids) - 1) . '?'; // 关键修改:这里选择的是 `name` 字段 $sql = "SELECT `uid`, `name` FROM `db_hk4e_user_gio`.`t_player_uid` WHERE `uid` IN ($placeholders)"; $stmt = $pdo->prepare($sql); $stmt->execute($uids); $playerMap = []; while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { $playerMap[$row['uid']] = $row['name']; // 使用 `name` 作为昵称 } } else { $playerMap = []; } } catch (PDOException $e) { $playerMap = []; error_log("跨库查询失败: " . $e->getMessage()); } $dsn = "mysql:host=$host;dbname=$dbname;charset=utf8mb4"; try { $pdo = new PDO($dsn, $username, $password); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } catch (PDOException $e) { die("数据库连接失败: " . $e->getMessage()); } // 设置分页参数 $limit = 10; $page = isset($_GET['page']) ? (int)$_GET['page'] : 1; $page = max(1, $page); $offset = ($page - 1) * $limit; // === 获取主键字段名 === function getPrimaryKey($pdo, $table) { $stmt = $pdo->prepare("DESCRIBE `$table`"); $stmt->execute(); $columns = $stmt->fetchAll(PDO::FETCH_ASSOC); foreach ($columns as $col) { if ($col['Key'] === 'PRI') { return $col['Field']; // 返回主键字段名 } } return null; // 没有主键 } $primaryKey = getPrimaryKey($pdo, 't_accounts'); if (!$primaryKey) { die("错误:t_accounts 表没有定义主键!"); } // 处理更新请求 if ($_POST['action'] === 'update') { $pkValue = $_POST[$primaryKey]; // 使用实际主键 // 获取所有字段 $stmt = $pdo->prepare("DESCRIBE `t_accounts`"); $stmt->execute(); $columns = $stmt->fetchAll(PDO::FETCH_COLUMN); $setParts = []; $params = []; foreach ($columns as $col) { if ($col !== $primaryKey && isset($_POST[$col])) { // 排除主键本身 $setParts[] = "`$col` = ?"; $params[] = $_POST[$col]; } } $params[] = $pkValue; // WHERE 条件:主键 = ? try { $sql = "UPDATE `t_accounts` SET " . implode(', ', $setParts) . " WHERE `$primaryKey` = ?"; $stmt = $pdo->prepare($sql); $stmt->execute($params); echo "<div style='color:green;'>主键为 <strong>$pkValue</strong> 的记录已成功更新!</div>"; } catch (PDOException $e) { echo "<div style='color:red;'>更新失败: " . $e->getMessage() . "</div>"; } } // 查询总行数 $totalStmt = $pdo->query("SELECT COUNT(*) FROM `t_accounts`"); $totalRows = $totalStmt->fetchColumn(); $totalPages = ceil($totalRows / $limit); // 获取当前页数据 $stmt = $pdo->prepare("SELECT * FROM `t_accounts` LIMIT ? OFFSET ?"); $stmt->bindValue(1, $limit, PDO::PARAM_INT); $stmt->bindValue(2, $offset, PDO::PARAM_INT); $stmt->execute(); $rows = $stmt->fetchAll(PDO::FETCH_ASSOC); // 获取字段名 $columns = empty($rows) ? [] : array_keys($rows[0]); ?> <!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8"> <title>动态编辑 t_accounts 数据表(智能主键)</title> <style> body { font-family: Arial, sans-serif; margin: 20px; background-color: #f7f7f7; } table { width: 100%; border-collapse: collapse; margin-top: 20px; background-color: #fff; } th, td { border: 1px solid #ccc; padding: 10px; text-align: left; } th { background-color: #007BFF; color: white; } input[type="text"], input[type="password"] { width: 100%; padding: 5px; box-sizing: border-box; } button.save { background-color: #28a745; color: white; border: none; padding: 5px 10px; cursor: pointer; } .pagination { margin: 20px 0; text-align: center; } .pagination a, .pagination span { display: inline-block; margin: 0 3px; padding: 8px 12px; text-decoration: none; border: 1px solid #ccc; background-color: #fff; color: #007BFF; } .pagination a:hover { background-color: #007BFF; color: white; } .pagination .current { background-color: #007BFF; color: white; font-weight: bold; } .pagination .ellipsis { color: #999; background-color: transparent; border: none; } </style> </head> <body> <h1>MySQL 数据表:t_accounts(自动识别主键)</h1> <p>本页面自动识别主键字段,支持安全更新。</p> <?php if (!empty($rows)): ?> <?php $visibleColumns = ['uid', 'name', 'email', 'type']; ?> <table> <thead> <tr> <?php foreach ($visibleColumns as $col): ?> <th><?= htmlspecialchars(ucfirst($col)) ?></th> <?php endforeach; ?> <th>玩家昵称</th> <!-- 新增列 --> <th>操作</th> </tr> </thead> <tbody> <?php foreach ($rows as $row): ?> <tr> <form method="post" style="display:inline;"> <input type="hidden" name="action" value="update"> <input type="hidden" name="<?= htmlspecialchars($primaryKey) ?>" value="<?= htmlspecialchars($row[$primaryKey]) ?>"> <!-- 显示白名单字段 --> <?php foreach ($visibleColumns as $col): ?> <td> <?php if ($col === $primaryKey): ?> <?= htmlspecialchars($row[$col]) ?> <input type="hidden" name="<?= htmlspecialchars($col) ?>" value="<?= htmlspecialchars($row[$col]) ?>"> <?php else: ?> <input type="text" name="<?= htmlspecialchars($col) ?>" value="<?= htmlspecialchars($row[$col] ?? '') ?>" style="width:100%; padding:5px;"> <?php endif; ?> </td> <?php endforeach; ?> <!-- 显示从 db_hk4e_user_gio.t_player_uid 查到的 nickname --> <td> <?php echo htmlspecialchars($playerMap[$row['uid']] ?? '<i style="color:#999;">未找到</i>'); ?> </td> <!-- 隐藏其他字段防止丢失 --> <?php foreach ($columns as $col): ?> <?php if (!in_array($col, $visibleColumns)): ?> <input type="hidden" name="<?= htmlspecialchars($col) ?>" value="<?= htmlspecialchars($row[$col] ?? '') ?>"> <?php endif; ?> <?php endforeach; ?> <td><button type="submit" class="save">保存</button></td> </form> </tr> <?php endforeach; ?> </tbody> </table> <!-- 分页导航:最多显示 3 个页码 --> <div class="pagination"> <?php if ($page > 1): ?> <a href="?page=<?= $page - 1 ?>">上一页</a> <?php else: ?> <span>上一页</span> <?php endif; ?> <?php // 设置最多显示 3 个页码 $maxVisible = 3; $half = (int)($maxVisible / 2); // 1 $start = $page - $half; $end = $page + $half; // 调整范围以确保始终显示 3 个(除非靠近边界) if ($start < 1) { $start = 1; $end = min($totalPages, $start + $maxVisible - 1); } if ($end > $totalPages) { $end = $totalPages; $start = max(1, $end - $maxVisible + 1); } ?> <!-- 首页 + 左侧省略号 --> <?php if ($start > 1): ?> <a href="?page=1">1</a> <?php if ($start > 2): ?> <span class="ellipsis">...</span> <?php endif; ?> <?php endif; ?> <!-- 显示最多 3 个页码 --> <?php for ($i = $start; $i <= $end; $i++): ?> <?php if ($i == $page): ?> <span class="current"><?= $i ?></span> <?php else: ?> <a href="?page=<?= $i ?>"><?= $i ?></a> <?php endif; ?> <?php endfor; ?> <!-- 尾页 + 右侧省略号 --> <?php if ($end < $totalPages): ?> <?php if ($end < $totalPages - 1): ?> <span class="ellipsis">...</span> <?php endif; ?> <a href="?page=<?= $totalPages ?>"><?= $totalPages ?></a> <?php endif; ?> <?php if ($page < $totalPages): ?> <a href="?page=<?= $page + 1 ?>">下一页</a> <?php else: ?> <span>下一页</span> <?php endif; ?> <span style="margin-left: 20px;">共 <?= $totalRows ?> 条记录,<?= $totalPages ?> 页</span> </div> <?php else: ?> <p>该表中没有数据。</p> <?php endif; ?> </body> </html> 无法连接db_hk4e_user_gio 数据库
最新发布
10-09
<?php $servername = "localhost"; $username = "root"; $password = "qq28009618"; $dbname = "syxywl.cn_sdk-14"; // 创建连接 $conn = new mysqli($servername, $username, $password, $dbname); // 检查连接 if ($conn->connect_error) { die("连接失败: " . $conn->connect_error); } // 设置字符集 $conn->set_charset("utf8"); // 分页配置 $items_per_page = 10; $page = isset($_GET['page']) ? (int)$_GET['page'] : 1; $page = max(1, $page); // 最小为第 1 页 $offset = ($page - 1) * $items_per_page; // 获取总行数 $total_sql = "SELECT COUNT(*) AS total FROM t_accounts"; $total_result = $conn->query($total_sql); $total_row = $total_result->fetch_assoc(); $total_items = $total_row['total']; $total_pages = ceil($total_items / $items_per_page); // 查询当前页数据 $sql = "SELECT * FROM t_accounts LIMIT $items_per_page OFFSET $offset"; $result = $conn->query($sql); if ($result === false) { die("查询出错: " . $conn->error); } // 获取字段信息 $fields = $result->fetch_fields(); echo "<h2>编辑账户数据(第 $page 页,共 $total_pages 页)</h2>"; echo "<table border='1' cellpadding='8' style='border-collapse: collapse; width: 100%;'>"; // 表头 echo "<tr>"; foreach ($fields as $field) { echo "<th style='background-color: #f0f0f0; text-align: left;'>".$field->name."</th>"; } echo "<th>操作</th>"; echo "</tr>"; // 数据行:每行一个 form while ($row = $result->fetch_assoc()) { $id = $row['id']; echo "<form action='save_row.php' method='post' style='display:inline;'>"; echo "<tr>"; foreach ($fields as $field) { $name = $field->name; $value = htmlspecialchars($row[$name]); echo "<td><input type='text' name='$name' value='$value' style='width: 100%;'></td>"; } // 操作列:隐藏 ID + 保存按钮 echo "<td>"; echo "<input type='hidden' name='id' value='$id'>"; echo "<input type='submit' value='保存' style='background-color: #4CAF50; color: white; border: none; padding: 5px 10px; cursor: pointer;'>"; echo "</td>"; echo "</tr>"; echo "</form>"; } echo "</table>"; // 分页导航 echo "<div style='margin-top: 20px;'>"; if ($page > 1) { echo "<a href='?page=" . ($page - 1) . "' style='margin-right: 10px;'>« 上一页</a>"; } else { echo "<span style='color: #ccc; margin-right: 10px;'>« 上一页</span>"; } echo "第 $page 页,共 $total_pages 页"; if ($page < $total_pages) { echo " <a href='?page=" . ($page + 1) . "' style='margin-left: 10px;'>下一页 »</a>"; } else { echo " <span style='color: #ccc; margin-left: 10px;'>下一页 »</span>"; } echo "</div>"; $conn->close(); ?> 点击保存无效
10-09
<?php // 配置数据库连接 $host = 'localhost'; $dbname = '`syxywl.cn_sdk-14`'; // 含特殊字符,用反引号包裹 $username = 'root'; $password = 'qq28009618'; $dsn = "mysql:host=$host;dbname=$dbname;charset=utf8mb4"; try { $pdo = new PDO($dsn, $username, $password); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } catch (PDOException $e) { die("数据库连接失败: " . $e->getMessage()); } // 设置分页参数 $limit = 10; // 每页显示 10 条 $page = isset($_GET['page']) ? (int)$_GET['page'] : 1; $page = max(1, $page); // 最小为第 1 页 $offset = ($page - 1) * $limit; // 处理更新请求 if ($_POST['action'] === 'update') { $id = $_POST['id']; // 获取所有字段名(排除主键 id) $stmt = $pdo->prepare("DESCRIBE `t_accounts`"); $stmt->execute(); $columns = $stmt->fetchAll(PDO::FETCH_COLUMN); $setParts = []; $params = []; foreach ($columns as $col) { if ($col !== 'id' && isset($_POST[$col])) { $setParts[] = "`$col` = ?"; $params[] = $_POST[$col]; } } $params[] = $id; try { $sql = "UPDATE `t_accounts` SET " . implode(', ', $setParts) . " WHERE id = ?"; $stmt = $pdo->prepare($sql); $stmt->execute($params); echo "<div style='color:green;'>ID 为 $id 的记录已成功更新!</div>"; } catch (PDOException $e) { echo "<div style='color:red;'>更新失败: " . $e->getMessage() . "</div>"; } } // 查询总行数以计算分页 $totalStmt = $pdo->query("SELECT COUNT(*) FROM `t_accounts`"); $totalRows = $totalStmt->fetchColumn(); $totalPages = ceil($totalRows / $limit); // 获取当前页的数据 $stmt = $pdo->prepare("SELECT * FROM `t_accounts` LIMIT ? OFFSET ?"); $stmt->bindValue(1, $limit, PDO::PARAM_INT); $stmt->bindValue(2, $offset, PDO::PARAM_INT); $stmt->execute(); $rows = $stmt->fetchAll(PDO::FETCH_ASSOC); // 获取字段名(列名) $columns = empty($rows) ? [] : array_keys($rows[0]); ?> <!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8"> <title>动态编辑 t_accounts 数据表(分页)</title> <style> body { font-family: Arial, sans-serif; margin: 20px; background-color: #f7f7f7; } table { width: 100%; border-collapse: collapse; margin-top: 20px; background-color: #fff; } th, td { border: 1px solid #ccc; padding: 10px; text-align: left; } th { background-color: #007BFF; color: white; } input[type="text"], input[type="password"], input[type="email"] { width: 100%; padding: 5px; box-sizing: border-box; } button.save { background-color: #28a745; color: white; border: none; padding: 5px 10px; cursor: pointer; } .pagination { margin: 20px 0; text-align: center; } .pagination a, .pagination span { display: inline-block; margin: 0 5px; padding: 8px 12px; text-decoration: none; border: 1px solid #ccc; background-color: #fff; color: #007BFF; } .pagination a:hover { background-color: #007BFF; color: white; } .pagination .current { background-color: #007BFF; color: white; font-weight: bold; } .message { margin: 10px 0; padding: 10px; border-radius: 4px; background-color: #d4edda; color: #155724; } </style> </head> <body> <h1>MySQL 数据表:t_accounts(分页显示,每页 10 行)</h1> <p>本页面自动识别表结构,支持分页与在线修改。</p> <?php if (!empty($rows)): ?> <table> <thead> <tr> <?php foreach ($columns as $col): ?> <th><?= htmlspecialchars($col) ?></th> <?php endforeach; ?> <th>操作</th> </tr> </thead> <tbody> <?php foreach ($rows as $row): ?> <tr> <form method="post" style="display:inline;"> <input type="hidden" name="action" value="update"> <input type="hidden" name="id" value="<?= htmlspecialchars($row['id']) ?>"> <?php foreach ($columns as $col): ?> <td> <?php if ($col === 'password'): ?> <input type="password" name="<?= htmlspecialchars($col) ?>" value="<?= htmlspecialchars($row[$col]) ?>" placeholder="保持原密码"> <?php else: ?> <input type="text" name="<?= htmlspecialchars($col) ?>" value="<?= htmlspecialchars($row[$col]) ?>"> <?php endif; ?> </td> <?php endforeach; ?> <td><button type="submit" class="save">保存</button></td> </form> </tr> <?php endforeach; ?> </tbody> </table> <!-- 分页导航 --> <div class="pagination"> <?php if ($page > 1): ?> <a href="?page=<?= $page - 1 ?>">上一页</a> <?php else: ?> <span>上一页</span> <?php endif; ?> <?php for ($i = 1; $i <= $totalPages; $i++): ?> <?php if ($i == $page): ?> <span class="current"><?= $i ?></span> <?php else: ?> <a href="?page=<?= $i ?>"><?= $i ?></a> <?php endif; ?> <?php endfor; ?> <?php if ($page < $totalPages): ?> <a href="?page=<?= $page + 1 ?>">下一页</a> <?php else: ?> <span>下一页</span> <?php endif; ?> <span style="margin-left: 20px;">共 <?= $totalRows ?> 条记录,<?= $totalPages ?> 页</span> </div> <?php else: ?> <p>该表中没有数据。</p> <?php endif; ?> </body> </html> 改为只显示5个跳转分页
10-09
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值