制作WEB版QQ多人聊天過程中, 無法將數(shù)據(jù)插入數(shù)據(jù)庫(kù),查找了多遍,實(shí)在不知哪里有錯(cuò)誤,請(qǐng)各位老師幫忙看一下,給指點(diǎn),非常感謝!下面是各文件:
//my.js
function getXmlHttpObject(){
var xmlHttpRequest;
if(window.ActiveXObject){
xmlHttpRequest=new ActiveXObject("Microsoft.XMLHTTP");
}else{
xmlHttpRequest=new XMLHttpRequest;
}
return xmlHttpRequest;
}
function $(id){
return document.getElementById(id);
}
<!-- login.php -->
<?php
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>登錄頁面</title>
</head>
<body style="text-align: center;">
<h1>歡迎登錄聊天室</h1>
<form action="LoginController.php" method="post">
用戶名: <input type="text" name="username" /><br /><br />
密 碼: <input type="password" name="passwd" /><br /><br />
<input type="submit" value="登錄聊天室" />
</form>
</body>
</html>
<!-- friendList.php -->
<?php
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>好友列表</title>
<script src="scripts/my.js"></script>
<script>
function change1(val,obj){
if(val=='over'){
obj.style.color="red";
obj.style.cursor="pointer";
}else if(val=='out'){
obj.style.color="black";
}
}
function openChatRoom(obj){
window.open("chatRoom.php?username="+obj.innerText,'',"width=300,height=500");
}
</script>
</head>
<body style="text-align: center;">
<h1>好友列表</h1>
<ul>
<li onmouseover="change1('over',this)" onmouseout="change1('out',this)" onclick="openChatRoom(this)">小張</li>
<li onmouseover="change1('over',this)" onmouseout="change1('out',this)" onclick="openChatRoom(this)">小王</li>
<li onmouseover="change1('over',this)" onmouseout="change1('out',this)" onclick="openChatRoom(this)">小劉</li>
</ul>
</body>
</html>
<!-- LoginController.php -->
<?php
$loginUser=$_POST['username'];
$pwd=$_POST['passwd'];
if($pwd=="123"){
session_start();
$_SESSION['loginuser']=$loginUser;
header("Location: friendList.php");
}else{
header("Location: login.php");
}
?>
<!-- chatRoom.php -->
<?php
?>
<!DOCTYPE html>
<html>
<head>
<?php
$username=$_GET['username'];
session_start();
$loginUser=$_SESSION['loginuser'];
?>
<meta charset="utf-8" />
<title>聊天室</title>
<script src="scripts/my.js"></script>
<script>
window.resizeTo(800,700);
function sendMessage(){
var myXmlHttpRequest=getXmlHttpObject();
if(myXmlHttpRequest){
url="SendMessageController.php";
var data="con="+$('con').value+"&getter=<?php echo $username ?>&sender=<?php echo $loginUser ?>";
alert(data);
myXmlHttpRequest.open("post",url,true);
myXmlHttpRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
myXmlHttpRequest.onreadystatechange=function (){
if(myXmlHttpRequest.readyState==4){
if(myXmlHttpRequest.status==200){
}
}
}
myXmlHttpRequest.send(data);
}
}
</script>
</head>
<body style="text-align: center;">
<h1>聊天室(<font color="red"><?php echo $loginUser ?></font>正在和<font color="red"><?php echo $username ?></font>聊天)</h1>
<form>
<textarea cols="50" rows="20"></textarea><br /><br />
<input type="text" style="width:300px;" id="con" />
<input type="button" value="發(fā)送信息" onclick="sendMessage()" />
</form>
</body>
</html>
<!-- SendMessageController.php -->
<?php
include_once 'MessageService.class.php';
$sender=$_POST['sender'];
$getter=$_POST['getter'];
$con=$_POST['con'];
// file_put_contents("d :/mylog.log",$sender."-".$getter."-".$con."\r\n",FILE_APPEND);
$messageService=new MessageService();
$res=$messageService->addMessage($sender, $getter, $con);
if($res==1){
}else{
echo "err";
}
?>
<!-- MessageService.class.php -->
<?php
include_once 'SqlHelper.class.php';
class MessageService {
function addMessage($sender, $getter, $con) {
$sql="insert into messages(sender,getter,content,sendTime)
values('$sender','$getter','$con',now())";
$sqlHelper=new SqlHelper();
return $sqlHelper->execute_dml($sql);
}
}
?>
<!-- SqlHelper.class.php -->
<?php
class SqlHelper {
public $conn;
public $dbname="chat";
public $username="root";
public $password="123";
public $host="localhost";
public function __construct(){
$this->conn=mysql_connect($this->host,$this->username,$this->password);
if(!$this->conn){
die("連接失敗".mysql_error());
}
mysql_select_db($this->dbname,$this->conn);
}
//執(zhí)行dql語句
public function execute_dql($sql){
$res=mysql_query($sql,$this->conn) or die(mysql_error());
return $res;
}
//執(zhí)行dql語句,但是返回的是一個(gè)數(shù)組
public function execute_dql2($sql){
$arr=array();
$res=mysql_query($sql,$this->conn) or die(mysql_error());
//把$res=>$arr 把結(jié)果集內(nèi)容轉(zhuǎn)移到一個(gè)數(shù)組中.
while($row=mysql_fetch_assoc($res)){
$arr[]=$row;
}
//這里就可以馬上把$res關(guān)閉.
mysql_free_result($res);
return $arr;
}
//考慮分頁情況的查詢,這是一個(gè)比較通用的并體現(xiàn)oop編程思想的代碼
//$sql1="select * from where 表名 limit 0,6";
//$sql2="select count(id) from 表名"
public function exectue_dql_fenye($sql1,$sql2,$fenyePage){
//這里我們查詢了要分頁顯示的數(shù)據(jù)
$res=mysql_query($sql1,$this->conn) or die(mysql_error());
//$res=>array()
$arr=array();
//把$res轉(zhuǎn)移到$arr
while($row=mysql_fetch_assoc($res)){
$arr[]=$row;
}
mysql_free_result($res);
$res2=mysql_query($sql2,$this->conn) or die(mysql_error());
if($row=mysql_fetch_row($res2)){
$fenyePage->pageCount=ceil($row[0]/$fenyePage->pageSize);
$fenyePage->rowCount=$row[0];
}
mysql_free_result($res2);
//把導(dǎo)航信息也封裝到fenyePage對(duì)象中
$navigate="";
if ($fenyePage->pageNow>1){
$prePage=$fenyePage->pageNow-1;
$navigate="<a href='{$fenyePage->gotoUrl}?pageNow=$prePage'>上一頁</a> ";
}
if($fenyePage->pageNow<$fenyePage->pageCount){
$nextPage=$fenyePage->pageNow+1;
$navigate.="<a href='{$fenyePage->gotoUrl}?pageNow=$nextPage'>下一頁</a> ";
}
$page_whole=10;
$start=floor(($fenyePage->pageNow-1)/$page_whole)*$page_whole+1;
$index=$start;
//整體每10頁向前翻
//如果當(dāng)前pageNow在1-10頁數(shù),就沒有向前翻動(dòng)的超連接
if($fenyePage->pageNow>$page_whole){
$navigate.=" <a href='{$fenyePage->gotoUrl}?pageNow=".($start-1)."'> << </a>";
}
//定$start 1---》10 floor((pageNow-1)/10)=0*10+1 11->20 floor((pageNow-1)/10)=1*10+1 21-30 floor((pageNow-1)/10)=2*10+1
for(;$start<$index+$page_whole;$start++){
$navigate.="<a href='{$fenyePage->gotoUrl}?pageNow=$start'>[$start]</a>";
}
//整體每10頁翻動(dòng)
$navigate.=" <a href='{$fenyePage->gotoUrl}?pageNow=$start'> >> </a>";
//顯示當(dāng)前頁和共有多少頁
$navigate.=" 當(dāng)前頁{$fenyePage->pageNow}/共{$fenyePage->pageCount}頁";
//把$arr賦給$fenyePage
$fenyePage->res_array=$arr;
$fenyePage->navigate=$navigate;
}
//執(zhí)行dml語句
public function execute_dml($sql){
$b=mysql_query($sql,$this->conn) or die(mysql_error());
if(!$b){
return 0; //失敗
}else{
if(mysql_affected_rows($this->conn)>0){
return 1;//表示執(zhí)行ok
}else{
return 2;//表示沒有行受到影響
}
}
}
//關(guān)閉連接的方法
public function close_connect(){
if(!empty($this->conn)){
mysql_close($this->conn);
}
}
}
?>
數(shù)據(jù)庫(kù)
create database chat;
create table messages(
id int unsigned primary key auto_increment,
sender varchar(64) not null,
getter varchar(64) not null,
content varchar(3600) not null,
sendTime datetime not null,
isGet tinyint default 0)
請(qǐng)老師們指點(diǎn),多謝!
- 4 回答
- 0 關(guān)注
- 441 瀏覽
添加回答
舉報(bào)
0/150
提交
取消