Py学习  »  DATABASE

php mysqli中的数据库

Timur • 4 年前 • 334 次点击  

我在PHP上写聊天,当我尝试登录时,会在会话中记录用户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>

这是我的用户聊天桌的结构:

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;
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/38643
 
334 次点击  
文章 [ 1 ]  |  最新文章 4 年前
user1334621
Reply   •   1 楼
user1334621    5 年前
  1. 你的结构是什么 用户聊天 表。我看不到您在插入数据库时为每条消息分配海报ID。你必须在你的 用户聊天 喜欢 海报ID 哪个是 用户数据 表的ID并在HANDLE POST请求中运行此SQL INSERT INTO users_chat (poster_id, usermsg, time) VALUES('{$_SESSION["user_id"]}', '{$usermsg}','{$time}')
  2. while($row_chat = mysqli_fetch_array($res)):?> <?php $_SESSION['user_id'] = $row_chat['id'];?>

    这会将会话中的用户ID修改为聊天消息的ID,我认为这不是您想要的。