PHP医疗咨询平台聊天系统:顾问端发送消息对齐与气泡样式异常问题求助
Hey there, let's dig into why your consultant's sent messages are showing up as received ones. This is a common type of issue, but let's break it down step by step to fix it.
First, let's recap the core problem: Your $isSender condition isn't evaluating to true when the consultant sends a message, even though you've confirmed the IDs match. Let's go through the most likely fixes:
1. Fix Type Mismatch with Explicit Integer Casting
Session variables (like $_SESSION['id']) are often stored as strings, while database sender_id values are almost always integers. Even though == does loose type comparison, using strict equality (===) with explicit casting will eliminate any hidden type-related bugs.
Update your $isSender line to this:
$isSender = (int)$chat['sender_id'] === (int)$consultant_id;
This ensures both values are treated as integers, so the comparison will work as expected every time.
2. Double-Check Session Initialization
It sounds obvious, but make sure you're starting the session at the very top of consultant_window.php before any output (HTML or whitespace):
<?php session_start(); // This must be the first line of code // Also add a check to ensure the consultant is logged in if (!isset($_SESSION['id'])) { header("Location: consultant_login.php"); exit; } $consultant_id = (int)$_SESSION['id']; // Rest of your code... ?>
If session_start() is missing, $_SESSION['id'] will be undefined, making $consultant_id null—so no way it can match the database's sender_id.
3. Add Debug Output to Confirm Values
To rule out any hidden surprises, add a debug line inside your message loop to print the actual values being compared. This will tell you exactly what's happening for each message:
// Inside your while loop, right before calculating $isSender echo "<div style='color: #ff0000; font-size: 12px;'>Sender ID: " . $chat['sender_id'] . " | Consultant ID: " . $consultant_id . " | Is Sender? " . ($isSender ? "YES" : "NO") . "</div>";
This will show you:
- If
$chat['sender_id']really matches$consultant_idfor the consultant's messages - Whether
$isSenderis correctly evaluating totruewhen it should
Once you confirm the values are correct and the condition works, you can remove this debug line.
4. Verify Message Insert Logic
Wait a second—when the consultant sends a message, are you correctly inserting their ID into the sender_id column of consultation_chat? It's easy to accidentally use the patient's ID instead of the consultant's when handling the send action.
Double-check the insert query on your message send endpoint for the consultant. It should look something like this:
// Example insert query for consultant's message $insertQuery = "INSERT INTO consultation_chat (consultation_id, sender_id, message) VALUES ($consultation_id, " . (int)$_SESSION['id'] . ", '" . mysqli_real_escape_string($conn, $message) . "')";
If you're using the wrong session variable here (like the patient's ID), the sender_id in the database will be incorrect, and your alignment logic will fail—even if everything else is right.
5. Clean Up Your Code for Readability (and Less Bugs)
Your current echo statement is hard to read and prone to escaping errors. Rewriting it with proper string separation will make it easier to debug and maintain. Here's an improved version:
$sender_id = (int)$chat['sender_id']; $isSender = $sender_id === (int)$consultant_id; $alignment = $isSender ? 'flex-end' : 'flex-start'; $bubbleClass = $isSender ? 'sent' : 'received'; $profilePic = htmlspecialchars($chat['profile_pic'], ENT_QUOTES); $messageText = htmlspecialchars($chat['message'], ENT_QUOTES); echo ' <div style="display: flex; justify-content: ' . $alignment . '; gap: 10px; align-items: flex-end;"> ' . (!$isSender ? '<img src="' . $profilePic . '" alt="Profile" style="width: 40px; height: 40px; border-radius: 50%;">' : '') . ' <div class="chat-message ' . $bubbleClass . '"> <p>' . $messageText . '</p> </div> ' . ($isSender ? '<img src="' . $profilePic . '" alt="Profile" style="width: 40px; height: 40px; border-radius: 50%;">' : '') . ' </div>';
This version:
- Escapes all dynamic content to prevent XSS attacks
- Uses single quotes for HTML strings to avoid double quote escaping
- Breaks the HTML into lines for easier reading
Start with steps 1 and 2—those are the most likely fixes for your issue. If those don't work, the debug output in step 3 will point you exactly where the problem is.
内容来源于stack exchange

