为每个聊天创建一个URL

时间:2018-09-11 17:15:17

标签: php

我有一个在线聊天。我希望每个用户创建自己的聊天,其他用户也可以加入。但是问题出在聊天网址上。如何确保在创建聊天时出现新的聊天和网址?

我的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>

1 个答案:

答案 0 :(得分:0)

我真的不想详细介绍如何创建它。但我会尽力解释。因此,如果用户创建聊天,您将在数据库中创建具有唯一ID的聊天记录。然后,您需要能够列出具有id作为属性的URL的可用聊天,例如/chat.php?chatId=<actual_chat_id>,然后您将能够创建一个能够使用$_GET['chatId']的chat.php文件,并且去获取聊天记录,并根据聊天记录从页面上的数据库中显示它。

相关问题