我有一个在线聊天。我希望每个用户创建自己的聊天,其他用户可以加入。但问题出在聊天url上。如何确保在创建聊天时,会出现新的聊天和url?
我的SQL:
CREATE TABLE IF NOT EXISTS `messages` (
`mes_id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL,
`mes_body` varchar(255) NOT NULL,
`chattime` bigint(20) NOT NULL,
PRIMARY KEY (`mes_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=13 ;
INSERT INTO `messages` (`mes_id`, `user_id`, `mes_body`, `chattime`) VALUES
(2, 8, 'go', 1370846012),
(3, 8, 'not go', 1370846029),
(4, 8, 'go', 1339742113),
(5, 8, 'hi dream', 1370241527),
(6, 3, 'ji', 1370846786),
(7, 3, 'nice job', 1370846856),
(8, 8, 'hi threr', 1370847094),
(9, 3, 'hi', 1370851056),
(10, 3, 'hi', 1370851838),
(11, 1, 'I have to go there ', 1370852157),
(12, 3, 'I have to make my ajax style more attractive and beautiful so be carefull about your style', 1370852536);
CREATE TABLE IF NOT EXISTS `user` (
`user_id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(250) NOT NULL,
`password` varchar(100) NOT NULL,
`online_status` int(11) NOT NULL,
PRIMARY KEY (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=9 ;
INSERT INTO `user` (`user_id`, `username`, `password`, `online_status`) VALUES
(1, 'jim', '123', 0),
(2, 'jack', '456', 0),
(3, 'dreams', '218837', 1),
(4, 'Tuhin', '789', 0),
(5, 'sadi', '456', 0),
(6, 'sadi', '456', 0),
(7, 'sadi', '456', 0),
(8, 'admin', '2013', 1);
CREATE TABLE `all_chats` (
`chat_id` int(11) NOT NULL,
`title_id` varchar(225) NOT NULL,
`mes_id` int(225) NOT NULL,
`user_id` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
ALTER TABLE `all_chats`
ADD PRIMARY KEY (`chat_id`,`user_id`);
ALTER TABLE `all_chats`
MODIFY `chat_id` int(11) NOT NULL AUTO_INCREMENT;COMMIT;下面是我的代码:
<?php
error_reporting ('E_ALL_E^NOTICE');
session_start();
require_once 'class/dbconnect.php';
require_once 'class/database.php';
require_once 'class/login.php';
$database=new database($db);
if(isset($_POST['action'])){
$login->check_login($_POST['username'],$_POST['password']);
}
$login->isLogin();
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<link href="cs/chat_style.css" rel="stylesheet" type="text/css">
<link href="cs/settings_chat.css" rel="stylesheet" type="text/css">
<title></title>
</head>
<body>
<?php
if (isset($_POST['adChat'])) {
$chat=$this->db->query("INSERT INTO `all_chats`(`title`, `chat_id`, `user_id`, `mes_id`)
VALUES (:chat_id,:title_id,:mes_id,:user_id)");
$chat->bindParam(":chat_id",$user_id,PDO::PARAM_INT);
$chat->bindParam(":title_id",$title,PDO::PARAM_STR);
$chat->bindParam(":mes_id",$message,PDO::PARAM_STR);
$chat->bindParam(":user_id",$user_id,PDO::PARAM_INT);
$insertMessage->chat();
}
?>
<div class="chat-settings">Настройки чата</div>
<div class="settings">
<form name="form" action="" method="post">
<input type="text" name="title">
<input type="submit" name="submit" value="Изменить название группы">
</form>
</div>
<input type="button" name="adChat" value="Создать чат" onclick="location.href='index.php?chat_id=<?php $chat_id ?>'">
<?php
if(isset($_POST['submit']))
{
extract($_POST);
$titleChange=$db->query("INSERT INTO `chat`(`title`) VALUES ('$title')");
} ?>
<div class="chat_container">
<div class="name"><?php echo $title; ?></div>
<div class="chat_box">
</div>
<div id="write_msg">
<textarea id="message" placeholder="Write Your Messge TO your Frientds"></textarea>
</div>
<a href="index.php?logout=11">Logout</a>
<div id="whoisonline">
<h3>Online now</h3>
<ul>
</ul>
</div>
</div>
</body>
</html>
发布于 2018-09-12 01:23:44
我真的不想详细介绍如何创建它。但我会试着解释的。因此,如果用户创建聊天,您将在数据库中创建具有唯一id的聊天记录。然后,您需要能够列出可用的聊天与urls,具有类似/chat.php?chatId=<actual_chat_id>的id属性,然后您将能够创建一个chat.php文件,该文件将能够使用$_GET['chatId']并获取聊天并将其从数据库中显示在页面上,具体取决于聊天内容。
https://stackoverflow.com/questions/52281059
复制相似问题