0

制作WEB版QQ多人聊天过程中, 无法将数据插入数据库,查找了多遍,实在不知哪里有错误,请各位老师帮忙看一下,给指点,非常感谢!
下面是各文件:

//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 />
密&nbsp;码: <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="发送信息" 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);
    }



    //执行dql语句
    public function execute_dql($sql){

        $res=mysql_query($sql,$this->conn) or die(mysql_error());
        return $res;

    }

    //执行dql语句,但是返回的是一个数组
    public function execute_dql2($sql){

        $arr=array();
        $res=mysql_query($sql,$this->conn) or die(mysql_error());

        //把$res=>$arr 把结果集内容转移到一个数组中.
        while($row=mysql_fetch_assoc($res)){
            $arr[]=$row;
        }
        //这里就可以马上把$res关闭.
        mysql_free_result($res);
        return $arr;

    }

    //考虑分页情况的查询,这是一个比较通用的并体现oop编程思想的代码
    //$sql1="select * from where 表名 limit 0,6";
    //$sql2="select count(id) from 表名"
    public function exectue_dql_fenye($sql1,$sql2,$fenyePage){

        //这里我们查询了要分页显示的数据
        $res=mysql_query($sql1,$this->conn) or die(mysql_error());
        //$res=>array()
        $arr=array();
        //把$res转移到$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);

        //把导航信息也封装到fenyePage对象中
        $navigate="";
        if ($fenyePage->pageNow>1){
            $prePage=$fenyePage->pageNow-1;
            $navigate="<a href='{$fenyePage->gotoUrl}?pageNow=$prePage'>上一页</a>&nbsp;";
        }
        if($fenyePage->pageNow<$fenyePage->pageCount){
            $nextPage=$fenyePage->pageNow+1;
            $navigate.="<a href='{$fenyePage->gotoUrl}?pageNow=$nextPage'>下一页</a>&nbsp;";
        }

        $page_whole=10;
        $start=floor(($fenyePage->pageNow-1)/$page_whole)*$page_whole+1;
        $index=$start;
        //整体每10页向前翻
        //如果当前pageNow在1-10页数,就没有向前翻动的超连接
        if($fenyePage->pageNow>$page_whole){
            $navigate.="&nbsp;&nbsp;<a href='{$fenyePage->gotoUrl}?pageNow=".($start-1)."'>&nbsp;&nbsp;<<&nbsp;&nbsp;</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页翻动
        $navigate.="&nbsp;&nbsp;<a href='{$fenyePage->gotoUrl}?pageNow=$start'>&nbsp;&nbsp;>>&nbsp;&nbsp;</a>";
        //显示当前页和共有多少页
        $navigate.=" 当前页{$fenyePage->pageNow}/共{$fenyePage->pageCount}页";

        //把$arr赋给$fenyePage
        $fenyePage->res_array=$arr;
        $fenyePage->navigate=$navigate;


    }

    //执行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;//表示执行ok
            }else{
                return 2;//表示没有行受到影响
            }
        }

    }

    //关闭连接的方法
    public function close_connect(){

        if(!empty($this->conn)){
            mysql_close($this->conn);
        }
    }
}
?>


数据库
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)

请老师们指点,多谢!

查看全部 3 个回答

0

1.首先,你在执行插入sql的之前,随便输出一下,看看是否有执行到插入语句。

2.如果有执行到语句,还没有数据插入,那么将拼接好的sql语句输出出来,然后将这个sql,去mysql单独执行一下,确认sql是否有问题。

我想经过这两步你应该能查出问题

推荐答案

0

已采纳

这个聊天程序是按着视频教程编写的,检查提交的数据也获取到了,将sql语句输出到mylog.log文件中,然后复制到mysql中,也执行成功了,反复检查,就是忽略了SqlHelper.class.php这个工具类文件,在这个文件中使用的是mysql扩展,而我安装的是php7.0版,此版本废弃了mysql扩展,使用mysqli扩展,只有改写这个工具类,才能连接数据库,插入数据。
谢谢各位老师的指教,非常感谢!