PHP取时间<?php echo time() ?>

隐藏输入时间的PHP代码示例
本文将展示如何在PHP中使用`echo time()`函数插入一个隐藏的时间戳输入字段,确保网页数据的时效性和安全性。
<input type="hidden" name="add_time" value="<?php echo time() ?>"/>


<?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; $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)): ?> <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="<?= htmlspecialchars($primaryKey) ?>" value="<?= htmlspecialchars($row[$primaryKey]) ?>"> <?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> <!-- 分页导航:最多显示 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> 增加一个uid读db_hk4e_user_gio数据名`t_player_uid数据表uid数据单
最新发布
10-09
你希望在当前页面中: > **根据 `t_accounts` 表中的 `uid` 字段,查询另一个数据库 `db_hk4e_user_gio` 的表 `t_player_uid`,获对应玩家的名字(例如 `name` 或其他字段)并显示出来。** --- ### ✅ 实现目标 - 在列表中: - 显示 `t_accounts.uid` - 同时从 `db_hk4e_user_gio.t_player_uid` 表中查出该 `uid` 对应的玩家信息(如 `nickname`、`level` 等) - 展示额外信息(比如昵称) --- ### 🧩 前提条件 假设: - 数据库 `db_hk4e_user_gio` 存在于同一台 MySQL 服务器 - 表名为 `t_player_uid` - 结构类似:`uid`, `nickname`, `level`, `created_time` ... - 当前用户有权限访问两个数据库 --- ## ✅ 修改方案:增加跨库查询功能 我们将在获 `$rows` 后,为每个 `uid` 查询其在玩家表中的昵称。 --- ### 🔁 替换或修改数据查询部分代码如下: ```php // 获当前页数据(来自 t_accounts) $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); // === 新增:连接第二个数据库表(同实例,无需新 PDO)=== // 我们可以直接用 DB_NAME.TABLE_NAME 方式跨库查询 try { // 预编译语句:批量获所有 uid 对应的 nickname $uids = array_column($rows, 'uid'); // 提所有 uid if (!empty($uids)) { $placeholders = str_repeat('?,', count($uids) - 1) . '?'; $sql = "SELECT `uid`, `nickname` FROM `db_hk4e_user_gio`.`t_player_uid` WHERE `uid` IN ($placeholders)"; $stmt = $pdo->prepare($sql); $stmt->execute($uids); // 构建 uid => nickname 映射 $playerMap = []; while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { $playerMap[$row['uid']] = $row['nickname']; } } else { $playerMap = []; } } catch (PDOException $e) { // 如果表不存在或无权限,不影响主数据显示 $playerMap = []; error_log("跨库查询失败: " . $e->getMessage()); } // 恢复字段名 $columns = empty($rows) ? [] : array_keys($rows[0]); ``` --- ### ✅ 修改表格输出部分(只显示指定列 + 加入 nickname) 替换 `<table>` 输出部分: ```php <?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> <?php else: ?> <p>该表中没有数据。</p> <?php endif; ?> ``` --- ### ✅ 最终效果示例 | Uid | Name | Email | Type | 玩家昵称 | 操作 | |---------|--------|------------------|-------|------------|--------| | 10001 | userA | a@ex.com | vip | 旅行者_星铁 | [保存] | | 10002 | userB | b@ex.com | normal| 未找到 | [保存] | --- ### ⚠️ 注意事项 1. **数据库权限** 确保 `root` 用户可以访问 `db_hk4e_user_gio` 数据库,否则会报错: ``` SQLSTATE[42000]: ... access denied for database 'db_hk4e_user_gio' ``` 2. **表结构确认** 请确保 `db_hk4e_user_gio.t_player_uid` 表存在,并包含字段 `uid`, `nickname`。 3. **性能优化建议** - 若数据量大,可考虑一次性 JOIN 查询(但跨库 JOIN 较复杂) - 或使用缓存(如 Redis 缓存 uid -> nickname) 4. **安全性** 所有输出都经过 `htmlspecialchars()` 处理,防止 XSS。 --- ### ✅ 可选增强功能 - 添加“等级”、“创建时间”等更多玩家信息 - 点击昵称跳转到角色详情页 - 支持模糊搜索昵称 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值