以下链接是用户收件箱的图片。
我正在尝试为接收来自其他用户的邮件的用户创建收件箱。我遇到的问题是,当userA向userB发送消息时,userB的收件箱会看到来自userA的消息。但是当userA向userB发送另一条消息时,userB会获得userA的两条新消息。例如:
userB'收件箱:
userA新消息 userA新消息
我试图避免两个相同的用户名,只显示一个。我已尝试按发件人用户名进行分组。它可以工作但是当userA向用户B发送消息时,新消息不会显示在收件箱中。我已经在这方面工作了一段时间,似乎无法找到解决方案。任何帮助,将不胜感激。以下是我的用户收件箱的源代码。
<?php
error_reporting(0);
//error_reporting(E_ALL);
//ini_set('display_errors', 1);
session_start();
$username = $_SESSION['username'];
$userid = $_SESSION['userid'];
?>
<!doctype html>
<html>
<head>
<title>Inbox</title>
<style>
body{
background-color: silver;
}
#ustatus{
color: green;
margin: 0 auto;
width: 750px;
}
#container{
border: 1px solid black;
width: 750px;
margin: 0 auto;
}
#container #content{
margin-top: 20px;
color: green;
}
</style>
</head>
<body>
<div id='ustatus'><b><?php echo "<a href='./members.php'>" . $username . " </a>"; ?></b></div>
<div id='container'>
<div id='content'>
<?php
$getid = $_GET['id'];
$getmsgid = $_GET['msgid'];
require_once("./connect.php");
/* $query = mysqli_query($con, "SELECT messages.id, messages.msg_id, messages.sender_id, messages.sender_user, messages.message, messages.open FROM messages INNER JOIN read_messages WHERE messages.receiver_id='".$userid."' AND read_messages.receiver_id='".$userid."'");
$read_msg_query = mysqli_query($con, "SELECT messages.id, messages.msg_id, messages.sender_id, messages.sender_user, messages.message, messages.open FROM messages INNER JOIN read_messages WHERE messages.receiver_id='".$userid."' AND read_messages.receiver_id='".$userid."' GROUP BY messages.sender_id, read_messages.sender_id"); */
$query = mysqli_query($con, "SELECT * FROM messages WHERE (LEAST (sender_id, receiver_id), GREATEST(sender_id, receiver_id)) IN (SELECT LEAST(sender_id, receiver_id) x, GREATEST(sender_id, receiver_id) y FROM messages GROUP BY x, y) AND '$userid' IN (receiver_id) ORDER BY id DESC")or die(mysqli_error($con));
$numrows = mysqli_num_rows($query);
if ($numrows > 0) {
while ($row = mysqli_fetch_assoc($query)) {
$dbid = $row['id'];
$dbmsgid = $row['msg_id'];
$dbsenderUser = $row['sender_user'];
$dbmessage = $row['message'];
$dbfromid = $row['sender_id'];
$dbopen = $row['open'];
if ($dbopen == 0) {
$open = "<div style='background-color: white;'>
<a href='./display_msg.php?msgid=$dbmsgid&id=$dbid'><b>" . $dbsenderUser . "</b></a><span style='margin-left: 200px;'>NEW</span>
<div style='clear: both;'></div>
</div>id: $dbid - open: $dbopen
<hr />";
} else {
$open = "<div style='background-color: silver;'>
<a href='./display_msg.php?msgid=$dbmsgid&id=$dbid'><b>" . $dbsenderUser . "</b></a>
<div style='clear: both;'></div>
</div>id: $dbid - open: $dbopen
<hr />";
}
echo $open;
}//end of while loop.
} else
echo "YOU DO NOT HAVE ANY MESSAGES AT THE MOMENT.";
?>
</div>
</div>
</body>
</html>
答案 0 :(得分:0)
尝试添加到sql查询LIMIT 1
$query = mysqli_query($con, "SELECT * FROM messages WHERE (LEAST (sender_id, receiver_id), GREATEST(sender_id, receiver_id)) IN (SELECT LEAST(sender_id, receiver_id) x, GREATEST(sender_id, receiver_id) y FROM messages GROUP BY x, y) AND '$userid' IN (receiver_id) ORDER BY id DESC LIMIT 1")or die(mysqli_error($con) );