Доброго времени суток , возникла проблема , при отправке сообщения , потом если обновить страницу , оно дублируется ( в датабазу тоже дублируется ) Код прилагаю . Помогите пожалуйста Код (Text): <?php session_start(); ini_set('display_errors', 'on'); 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"]; 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))) { header("location:./view-profiles.php"); exit; } 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(); } } } ?>