我尝试在网上建立聊天室的教程。
首先,我在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
发布于 2012-06-27 03:39:44
确保您阅读了有关SQL注入的内容
$words在哪里定义?
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);
}你应该用一些东西来定义这些。不知道还能告诉你什么,而不知道会出现什么样的错误。这就是我要开始的地方..让这个块看起来像这样
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);
}
?>发布于 2012-06-27 03:38:50
将speak.php中的代码更改为:
<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中。
https://stackoverflow.com/questions/11214607
复制相似问题