首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >建立简单聊天室时发送消息失败

建立简单聊天室时发送消息失败
EN

Stack Overflow用户
提问于 2012-06-27 03:27:59
回答 2查看 84关注 0票数 0

我尝试在网上建立聊天室的教程。

首先,我在MYSQL中创建了一个名为chatroom的数据库,并创建了一个名为chat的数据表,其中包含三列:chtime、nick、word。

然后我写了四个PHP文件,login.php,main.php,display.php,speak.php,但是关于显示和说话的问题。我的语音不起作用,我只是弹出一个新的窗口,没有任何文字。

我不知道问题出在哪里?

我试着修了几天,但都是白费力气。我的错误在哪里?

以下是我的代码:

Login.php http://codepad.org/WIfr3quz

Main.php http://codepad.org/b9pXuNl0

Display.php http://codepad.org/o7pf5G57

Speak.php http://codepad.org/wFDEMrNk

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-06-27 03:39:44

确保您阅读了有关SQL注入的内容

$words在哪里定义?

代码语言:javascript
复制
    if ($words){
    $link = mysqli_connect('localhost', 'xxx', 'xxx', 'ChatRoom');
        $time = date('Y-m-d-a:i:s');
        $str = "INSERT INTO chat(chtime,nick,words) values('$time','$nick','$words')" ;
        mysqli_query($str,$link);
        mysqli_close($link);
}

你应该用一些东西来定义这些。不知道还能告诉你什么,而不知道会出现什么样的错误。这就是我要开始的地方..让这个块看起来像这样

代码语言:javascript
复制
if(isset($_POST['words']))
    $link = mysqli_connect('localhost', 'xxx', 'xxx', 'ChatRoom');
        $time = date('Y-m-d-a:i:s');
            $nick = 'NickName';//However you would get the nick for the user
            $words = $link->real_escape_string($_POST['words']);
        $str = "INSERT INTO chat(chtime,nick,words) values('$time','$nick','$words')" ;
        mysqli_query($str,$link);
        mysqli_close($link);
}
?>
票数 1
EN

Stack Overflow用户

发布于 2012-06-27 03:38:50

将speak.php中的代码更改为:

代码语言:javascript
复制
<html> 
<head> 
<title>Speak</title> 
</head> 
<body> 
<?php 
    if ($words){
    $link = mysqli_connect('localhost', 'xxx', 'xxx', 'ChatRoom');
        $time = date('Y-m-d-a:i:s');
        $nick = $link->real_escape_string($_POST['nick']);
        $words = $link->real_escape_string($_POST['words']);
        $str = "INSERT INTO chat(chtime,nick,words) values('$time','$nick','$words')" ;
        mysqli_query($str,$link);
        mysqli_close($link);
}
?>
<form action = "Speak.php" method = "post" target = " _self"> 
<input type = "text" name = "nick"> 
<input type = "text" name = "words">
<input type = "submit" value = "Speak"> 
</form> 
</body> 
</html>

使用real_escape_string可以防止SQL代码注入。

POST表单发送的值存储在$_POST中。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11214607

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档