PHP: <?php session_start(); require_once("./Connector/DbConnectorPDO.php"); require("./helper/helperFunctions.php"); $userId = isset($_SESSION["userId"]) && !IsVariableIsSetOrEmpty($_SESSION["userId"]) ? $_SESSION["userId"] : 0; $connection = getConnection(); $userObj = $userId !== 0 && !IsVariableIsSetOrEmpty($_SESSION["user"]) ? $_SESSION["user"] : ""; $msgList = []; $recentMsgList = []; $msgToUserId = isset($_GET["id"]) && !IsVariableIsSetOrEmpty($_GET["id"]) ? $_GET["id"] : 0; if (isset($_POST["SendMessage"]) && !IsVariableIsSetOrEmpty($_POST["SendMessage"])) { $msg = $_POST["msg"]; { header("Location: " . $_SERVER["REQUEST_URI"]); } if (isset($msg) && !IsVariableIsSetOrEmpty($msg)) { $insertMessageQuery = "INSERT INTO messages(msg_from_user_id,msg,msg_to_user_Id,msg_date,is_msg_read) values(:userId,:msg,:msgToUserId,NOW(),0)"; $insertStmt = $connection->prepare($insertMessageQuery); $insertStmt->bindParam(':userId', $userId); $insertStmt->bindParam(':msg', $msg); $insertStmt->bindParam(':msgToUserId', $msgToUserId); $insertStmt->execute(); } } if ($userId === 0 || (!isset($_GET["id"]) && !isset($msgTouserId))) { } if ($msgToUserId !== 0) { $recentMsgQuery = "SELECT * FROM ( SELECT profile .id, profile.firstName, profile.lastName, profile.imgUrl, ( SELECT msg FROM messages WHERE messages.msg_from_user_id = profile.id OR messages.msg_to_user_id = profile.id ORDER BY id DESC LIMIT 1 ) AS lastMessage, ( SELECT msg_date FROM messages WHERE messages.msg_from_user_Id = profile.Id OR messages.msg_to_user_Id = profile.Id ORDER BY id DESC LIMIT 1 ) AS msgDate FROM profile WHERE id <> :userId ) X LEFT JOIN ( SELECT DISTINCT ids from ( select msg_from_user_id as ids from messages where msg_from_user_id =:userId or msg_to_user_id=:userId UNION select msg_to_user_id as ids from messages where msg_from_user_id =:userId or msg_to_user_id=:userId )uniqueIdList where ids <> :userId )IdList on X.id=IdList.ids WHERE ids IS NOT NULL AND lastMessage IS NOT NULL ORDER BY msgDate desc"; $recentQueryStmt = $connection->prepare($recentMsgQuery); $recentQueryStmt->bindParam(':userId', $userId); $recentQueryStmt->execute(); $recentMsgList = $recentQueryStmt->fetchAll(); $query = "SELECT m.id as msg_id ,msg_from_user_id ,msg_to_user_id ,msg ,msg_date ,is_msg_read ,msg_read_date ,fromUser.id as fromUserId ,fromUser.firstName as fromFirstName ,fromUser.lastName as fromLastName ,fromUser.imgUrl as fromUserImgUrl ,toUser.id as toUserId ,toUser.firstName as toUserFirstName ,toUser.lastName as toUserLastName ,toUser.imgUrl as toUserImgUrl FROM messages m LEFT JOIN profile as fromUser on m.msg_from_user_id=fromUser.id LEFT JOIN profile as toUser on m.msg_to_user_id=toUser.id WHERE (msg_from_user_id =:userId and msg_to_user_id=:sentToUserId) or (msg_from_user_id=:sentToUserId and msg_to_user_id=:userId)"; //ORDER BY msg_date DESC"; $stmt = $connection->prepare($query); $stmt->bindParam(':userId', $userId); $stmt->bindParam(':sentToUserId', $_GET["id"]); $stmt->execute(); $msgList = $stmt->fetchAll(); if (count($msgList) > 0) { $lastMsgRow = end($msgList); if ($lastMsgRow["msg_from_user_id"] === $msgToUserId && $lastMsgRow["msg_to_user_id"] === $userId && intval($lastMsgRow["is_msg_read"]) === 0) { $updateAllMsgReadQuery = "UPDATE messages set is_msg_read=1,msg_read_date=NOW() where msg_from_user_id=:sentToUserId and msg_to_user_id=:userId and is_msg_read=0"; $stmt = $connection->prepare($updateAllMsgReadQuery); $stmt->bindParam(':sentToUserId', $msgToUserId); $stmt->bindParam(':userId', $userId); $stmt->execute(); } } } ?> HTML: <div class="type_msg"> <div class="input_msg_write"> <form action="chat-users.php?id=<?= $msgToUserId ?>" method="post"> <input type="text" name="msg" class="write_msg" placeholder="Введите сообщение" required/> <button id="sendBtn" class="msg_send_btn" type="button"> <i class="fa fa-paper-plane-o" aria-hidden="true"></i> </button> <input type="Submit" id="sendMessage" style="display: none;" value="Send" name="SendMessage"/> </form> </div> </div>