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

PHP技术求助:登录后无法调取对应用户档案,需通过邮箱获取特定用户

Hey there! Let's fix this frustrating issue where you're pulling the last user's info instead of the logged-in one. The core fix here is to target the specific logged-in user's email (since you want to use their registration email) to fetch exactly their profile data from the database. Here's a step-by-step solution tailored to your case:

Step 1: Ensure You're Storing the Logged-In User's Email in Session

First, when a user successfully logs in, you need to save their email (or unique identifier) in the PHP session. This lets you access who's logged in across pages.

Add this right after validating their login credentials (e.g., checking their password matches the database):

// Start the session (must be at the TOP of every page using sessions, before any HTML/output)
session_start();

// After confirming the user's login is valid:
$user_email = "the_user_email_from_database"; // Replace with the actual email from your login query
$_SESSION['user_email'] = $user_email;
Step 2: Modify Your Database Query to Target the Logged-In User

Your original query was probably fetching all users and then grabbing the last one—we need to filter it to only the logged-in user's record.

Example with MySQLi (most common for beginners):

session_start();

// First, check if the user is actually logged in
if (!isset($_SESSION['user_email'])) {
    header("Location: login.php"); // Redirect to login if not logged in
    exit;
}

// Connect to your database (update credentials to match yours)
$conn = mysqli_connect("localhost", "your_db_username", "your_db_password", "your_db_name");

// Get the logged-in user's email from session
$target_email = $_SESSION['user_email'];

// Use a prepared statement to prevent SQL injection (critical for security!)
$stmt = mysqli_prepare($conn, "SELECT * FROM users WHERE email = ?");
mysqli_stmt_bind_param($stmt, "s", $target_email); // "s" means we're passing a string
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);

// Fetch the user's profile data
$current_user = mysqli_fetch_assoc($result);

// Handle edge case (shouldn't happen if login works, but good to have)
if (!$current_user) {
    echo "Error: User profile not found!";
    exit;
}

Alternative for PDO users:

session_start();

if (!isset($_SESSION['user_email'])) {
    header("Location: login.php");
    exit;
}

$pdo = new PDO("mysql:host=localhost;dbname=your_db_name", "your_db_username", "your_db_password");
$target_email = $_SESSION['user_email'];

$stmt = $pdo->prepare("SELECT * FROM users WHERE email = ?");
$stmt->execute([$target_email]);
$current_user = $stmt->fetch(PDO::FETCH_ASSOC);
Step 3: Display the Logged-In User's Profile

Now you can safely use the $current_user array to show their specific info:

echo "<h2>Your Profile</h2>";
echo "<p>Name: " . $current_user['full_name'] . "</p>";
echo "<p>Email: " . $current_user['email'] . "</p>";
echo "<p>User ID: " . $current_user['id'] . "</p>";
// Add any other profile fields you have in your database (e.g., bio, join date)
Quick Checks to Avoid Mistakes
  • Always call session_start() before any output (HTML tags, echo statements, etc.)—otherwise sessions won't work.
  • Double-check that your users table has a unique email constraint (this prevents duplicate registrations and ensures your query only returns one user).
  • Verify that during login, you're storing the correct email from the database (not a hardcoded value or user input that wasn't validated).

内容的提问来源于stack exchange,提问作者پردیس زریاب

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.14 08:32:40