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

PHP实现NRIC登录后从MySQL获取并显示用户全名问题求助

解决NRIC登录后显示用户全名的问题

Hey there! Let's fix that welcome message issue you're having after switching to NRIC for login. This is a super common hiccup when changing your login identifier, but we can work through it step by step.

1. 检查登录验证的SQL查询

The most likely culprit is that your login SQL is still targeting the wrong identifier, or not fetching the fullname when verifying the user.

When you used fullname as the login ID, your query probably looked like this:

$sql = "SELECT password, fullname FROM users WHERE fullname = ?";

Now that you're using NRIC, update the WHERE clause to target the nric column instead—don't forget to still select the fullname (we need it for the welcome message):

// Updated query for NRIC login
$sql = "SELECT password, fullname FROM users WHERE nric = ?";

Make sure you're binding the correct input value here: use the user's submitted NRIC (e.g., $_POST['nric']) instead of the old fullname variable.

2. 登录成功后存储全名到Session

Once the login is verified, save the user's fullname in the session so your homepage can access it. Remember to start the session first (this needs to go before any output on your login page):

session_start();

// After verifying the password matches...
if (password_verify($_POST['password'], $db_stored_password)) {
    // Store the fullname in session
    $_SESSION['user_fullname'] = $user_row['fullname'];
    // Optional: Store NRIC too if you need it elsewhere
    $_SESSION['user_nric'] = $user_row['nric'];
    
    // Redirect to homepage
    header("Location: homepage.php");
    exit;
} else {
    echo "Invalid NRIC or password. Please try again.";
}

3. 在主页显示欢迎信息

On your homepage, start the session again, check if the user is logged in, then output the fullname. We use htmlspecialchars() to prevent XSS attacks—always a good practice!

session_start();

// Redirect to login if user isn't logged in
if (!isset($_SESSION['user_fullname'])) {
    header("Location: login.php");
    exit;
}
?>

<!DOCTYPE html>
<html>
<head>
    <title>主页</title>
</head>
<body>
    <h1>欢迎,<?php echo htmlspecialchars($_SESSION['user_fullname']); ?>!</h1>
    <!-- Rest of your homepage content -->
</body>
</html>

快速排查清单

If it's still not working, double-check these:

  • Does your users table actually have a nric column with valid data matching what users are inputting?
  • Are you correctly capturing the NRIC from the login form? Make sure your form input has name="nric".
  • Is the session being started on both the login page and homepage before any HTML output?
  • Are there any typos in column names (e.g., nric vs NRIC—MySQL is case-insensitive on most systems, but better to match exactly)?

That should get your welcome message working again with NRIC login!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 03:46:30