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

PHP+MySQL实现带计算字段的用户临时信息展示页面求助

解决方案:实现用户注册数据展示表格

以下是完整的展示页面实现代码,包含数据库查询、额外字段计算和HTML表格渲染的全流程:

1. 完整代码实现

<?php
// 数据库连接配置(替换为你的实际信息)
$host = 'localhost';
$user = 'your_db_username';
$pass = 'your_db_password';
$dbname = 'your_db_name';
$tableName = 'your_table_name'; // 存储用户数据的表名

// 创建数据库连接
$conn = mysqli_connect($host, $user, $pass, $dbname);
if (!$conn) {
    die("连接失败: " . mysqli_connect_error());
}

// 查询所有用户数据
$sql = "SELECT firstName, lastName, phoneNumber, birthdate FROM {$tableName}";
$result = mysqli_query($conn, $sql);

// 定义诞生石映射数组
$birthstones = [
    'January' => 'Garnet',
    'February' => 'Amethyst',
    'March' => 'Aquamarine',
    'April' => 'Diamond',
    'May' => 'Emerald',
    'June' => 'Pearl',
    'July' => 'Ruby',
    'August' => 'Peridot',
    'September' => 'Sapphire',
    'October' => 'Opal',
    'November' => 'Topaz',
    'December' => 'Turquoise'
];
?>

<!DOCTYPE html>
<html>
<head>
    <title>注册用户列表</title>
    <style>
        table { border-collapse: collapse; width: 90%; margin: 2rem auto; }
        th, td { border: 1px solid #ddd; padding: 0.8rem; text-align: left; }
        th { background-color: #f5f5f5; font-weight: bold; }
        .empty-row { text-align: center; color: #666; }
    </style>
</head>
<body>
    <h2 style="text-align: center; margin-top: 2rem;">截至今日(<?php echo date('Y年m月d日'); ?>),以下参与者已注册</h2>
    
    <table>
        <thead>
            <tr>
                <th>名字</th>
                <th>姓氏</th>
                <th>电话号码</th>
                <th>区号</th>
                <th>资格状态</th>
                <th>出生日期</th>
                <th>出生月份</th>
                <th>诞生石</th>
            </tr>
        </thead>
        <tbody>
            <?php
            if (mysqli_num_rows($result) > 0) {
                // 循环处理每条用户数据
                while($row = mysqli_fetch_assoc($result)) {
                    // 提取区号:过滤非数字后取前3位(兼容带括号/符号的号码格式)
                    $cleanPhone = preg_replace('/[^0-9]/', '', $row['phoneNumber']);
                    $areaCode = substr($cleanPhone, 0, 3);
                    
                    // 判断资格状态
                    $eligibility = $areaCode === '425' ? 'yes' : 'no';
                    
                    // 获取出生月份全称
                    $birthMonth = date('F', strtotime($row['birthdate']));
                    
                    // 匹配诞生石
                    $birthstone = $birthstones[$birthMonth] ?? 'Unknown';
            ?>
            <tr>
                <td><?php echo htmlspecialchars($row['firstName']); ?></td>
                <td><?php echo htmlspecialchars($row['lastName']); ?></td>
                <td><?php echo htmlspecialchars($row['phoneNumber']); ?></td>
                <td><?php echo htmlspecialchars($areaCode); ?></td>
                <td><?php echo $eligibility; ?></td>
                <td><?php echo htmlspecialchars($row['birthdate']); ?></td>
                <td><?php echo $birthMonth; ?></td>
                <td><?php echo $birthstone; ?></td>
            </tr>
            <?php
                }
            } else {
                echo '<tr><td colspan="8" class="empty-row">暂无注册用户</td></tr>';
            }
            // 关闭数据库连接
            mysqli_close($conn);
            ?>
        </tbody>
    </table>
</body>
</html>

关键细节说明

  • 区号提取:先用preg_replace过滤电话号码中的非数字字符,再用substr取前3位,兼容带括号、破折号等格式的号码。
  • 出生月份转换:通过strtotime把数据库中的日期字符串转为时间戳,再用date('F')获取英文月份全称,匹配诞生石数组的键名。
  • 安全输出:所有用户输入的数据都用htmlspecialchars处理,防止XSS攻击。
  • 空数据处理:当数据库无数据时,显示提示行,避免表格结构断裂。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.05 11:15:01