title: User to user Private Messaging System
body: <p>The below link is an image for the user inbox.</p>
  
  <p><img src="https://i.stack.imgur.com/hUKxK.png" alt="enter image description here"></p>
  
  <p>I'm trying to create an inbox for a user that receives messages from other users. The problem I'm having is that when userA sends a message to userB, userB's inbox sees a message from userA. But when userA sends userB another message, userB get two new message of userA. For example:</p>
  
  <p>userB's Inbox:</p>
  
  <p>userA new message
  userA new message</p>
  
  <p>I'm trying to avoid two of the same username and to just display one. I've tried grouping by the sender username. It works but then when userA sends userB a message, the new message doesn't display in the inbox. I've been working on this for a while now and can't seem to fin a solution. Any help would be appreciated. Below is my source code for the user inbox.</p>
  
  <pre><code>&lt;?php
  error_reporting(0);
  //error_reporting(E_ALL);
  //ini_set('display_errors', 1);
  session_start();
  $username = $_SESSION['username'];
  $userid = $_SESSION['userid'];
  ?&gt;
  &lt;!doctype html&gt;
  &lt;html&gt;
      &lt;head&gt;
          &lt;title&gt;Inbox&lt;/title&gt;
          &lt;style&gt;
              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;
              }
          &lt;/style&gt;
      &lt;/head&gt;
      &lt;body&gt;
          &lt;div id='ustatus'&gt;&lt;b&gt;&lt;?php echo "&lt;a href='./members.php'&gt;" . $username . "        &lt;/a&gt;"; ?&gt;&lt;/b&gt;&lt;/div&gt;
          &lt;div id='container'&gt;
              &lt;div id='content'&gt;
                  &lt;?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 &gt; 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 = "&lt;div style='background-color: white;'&gt;
                                           &lt;a href='./display_msg.php?msgid=$dbmsgid&amp;id=$dbid'&gt;&lt;b&gt;" . $dbsenderUser . "&lt;/b&gt;&lt;/a&gt;&lt;span style='margin-left: 200px;'&gt;NEW&lt;/span&gt;
  
                                           &lt;div style='clear: both;'&gt;&lt;/div&gt;
                                           &lt;/div&gt;id:&amp;nbsp;$dbid - open:&amp;nbsp;$dbopen
                                            &lt;hr /&gt;";
                          } else {
                              $open = "&lt;div style='background-color: silver;'&gt;
                                           &lt;a href='./display_msg.php?msgid=$dbmsgid&amp;id=$dbid'&gt;&lt;b&gt;" . $dbsenderUser . "&lt;/b&gt;&lt;/a&gt;
  
                                           &lt;div style='clear: both;'&gt;&lt;/div&gt;
                                           &lt;/div&gt;id:&amp;nbsp;$dbid - open:&amp;nbsp;$dbopen
                                           &lt;hr /&gt;";
                          }
  
                          echo $open;
                      }//end of while loop.
                  } else
                      echo "YOU DO NOT HAVE ANY MESSAGES AT THE MOMENT.";
                  ?&gt;
              &lt;/div&gt;
          &lt;/div&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>