Я пишу чат на php, когда я пытаюсь войти, user_id записывается в сеанс, после успешной попытки появляется страница чата, где пишутся все сообщения, но проблема в том, что все сообщения были написаны по мне, но это не так. Есть несколько сообщений, которые были написаны другими пользователями. Итак, как я могу решить эту проблему с помощью JOIN? Можете ли вы дать мне немного рекламы или улучшить мой код?
Это код для входа в систему:
<?php
session_start();
include 'db.php';
if (isset($_POST['email-e']) && isset($_POST['password-pass'])) {
$email_e = mysqli_real_escape_string($mysqli,$_POST['email-e']);
$password_pass = crypt($_POST['password-pass']);
$query = "SELECT id, email, password FROM users_data WHERE email = '$email_e' AND password = '$password_pass'";
$sql = mysqli_query($mysqli,$query) or die(mysqli_error());
if (mysqli_num_rows($sql) == 1) {
$row = mysqli_fetch_assoc($sql);
$_SESSION['user_id'] = $row['id'];
$_SESSION['email-e'] = $row['email'];
setcookie("CookieMy", $row['email'], time()+60*60*24*10);
}
else {
echo 'User not found!';
header("Location: login.html");
}
}
if (isset($_SESSION['email-e'])){
header("Location: chat.php");
} else {
$email_e = '';
if (isset($_COOKIE['CookieMy'])){
$email_e = htmlspecialchars($_COOKIE['CookieMy']);
}
}
?>
Это код chat.php:
<?php
session_start();
include 'db_chat.php';
// header('Content-Type: text/html; charset=utf-8');
//echo trim($_SESSION['email-e'])." <br />"."You are authorized <br />";
?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Chat</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
<title>Chat</title>
<style>
form,p,span{margin:0;padding:0}
input{font:12px arial}
a{color:#00f;text-decoration:none}
a:hover{text-decoration:underline}
#wrapper,
#loginform{margin:0 auto;padding-bottom:25px;background:#ebf4fb;width:504px;border:1px solid #acd8f0}#loginform{padding-top:18px}#loginform p{margin:5px}
#chatbox{text-align:left;margin:0 auto;margin-bottom:25px;padding:10px;background:#fff;height:270px;width:430px;border:1px solid #acd8f0;overflow:auto}
#usermsg{
width:380px;
height: 50px;
border:1px solid #acd8f0;
border-radius: 3px 3px 3px 3px;
}
#submitmsg{width:70px; height: 53px; border-radius: 5px 5px 5px 5px; cursor: pointer;}
.error{color:#f00}
#menu{padding:12.5px 25px 12.5px 25px}
.welcome{float:left}
.logout{float:right}
.msgln{margin:0 0 2px 0}
</style>
</head>
<body>
<div id="wrapper">
<div id="menu">
<p class="welcome">Welcome, <?php echo trim($_SESSION['email-e'])?><b></b></p>
<p class="logout" name="logout">
<a name="logout" id="exit" href="#">Exit Chat</a>
</p>
<div style="clear:both"></div>
</div>
<div id="chatbox">
<?php
if(isset($_POST['submitmsg'])) {
if(!empty($_POST['usermsg']) && is_string($_POST['usermsg'])) {
$time = date("Y-m-d H:i:s");
$usermsg = trim($_POST['usermsg']);
$sql_chat ="INSERT INTO `users_chat` (usermsg,time)
VALUES('{$usermsg}','{$time}')";
$res = mysqli_query($mysqli_chat,$sql_chat);
}
}
$query_chat = "SELECT * FROM `users_chat`";
$res = mysqli_query($mysqli_chat,$query_chat);
while($row_chat = mysqli_fetch_array($res)):?>
<?php $_SESSION['user_id'] = $row_chat['id'];?>
Email: <?=$row_chat['email-e']?> <br>
Message: <?=$row_chat['usermsg']?> <br>
Time: <?=$row_chat['time']?> <br>
<?php endwhile;?>
</div>
<form name="message" action="" method="post">
<input name="usermsg" type="text" id="usermsg" style="margin-left:25px;">
<input name="submitmsg" type="submit" id="submitmsg" value="Send"/><br>
</form>
</div>
</body>
</html>
Это структура моей таблицы users_chat:
CREATE TABLE `users_chat` (
`id` int(11) NOT NULL,
`username` varchar(255) NOT NULL,
`usermsg` varchar(255) NOT NULL,
`user_id` int(11) NOT NULL,
`time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8;