开始学习PHP,以前用的是安装包,很容易出现各种各样的问题,于是便开始尝试着自己配置,花了两天时间终于配置成功,其间酸楚不足为外人道也,通过自己的尝试学到了很多东西,这两天又想在自己的电脑上运行一个留言本程序,通过排错纠错,终于在群里几位大哥的帮助下运行通过了这个简易留言本程序,呵呵,这年头什么都不容易啊!
<!--sys_conf.inc:系统配置文件-------->
<?php
$DBHOST="localhost";
$DBUSER="root";
$DBPWD="123456";
$DBNAME="my_chat";
?>
数据库:
CREATE DATABASE 'my_chat';
CREATE TABLE 'chat'(
'cid' INT NOT NULL AUTO_INCREMENT,
'author' VARCHAR(50) NOT NULL,
'create_time' TIME NOT NULL,
'text' BLOB NOT NULL,
PRIMARY KEY('cid')
);
聊天室用户登录页面:
<!--login.php:MY 聊天室用户登录页面------->
<html>
<head>
<title>用户登录</title>
</head>
<body>
<center>
<h1>欢迎来到MY聊天室</h1>
<h2>请输入您的昵称</h2>
<form action="main.php" method="POST" target="_self">
<input type="text" name="nick" cols="20">
<input type="submit" value="登录">
</form>
</center>
</body>
</html>
聊天室主页面:
<?php
session_start();
$user_name=$_POST["nick"];
session_register("user_name");
?>
<!--chat.php:MY 聊天室主页面------->
<html>
<title>MY聊天室主页面</title>
<frameset rows="80%,*">
<frame src="chat_display.php" name="chat_display">
<frame src="speak.php" name="speak">
</frameset><noframes></noframes>
</html>
用户发言页面:
<?php
session_start();
?>
<!--speak.php;用户发言页面--->
<html>
<head>
<title>发言</title>
</head>
<body>
<?php
print_r($_POST);
require_once("sys_conf.inc");
if($_POST["text"])
{
$text = $_POST["text"];
$link_id=mysql_connect($DBHOST,$DBUSER,$DBPWD);
mysql_select_db($DBNAME);
$time=date("h:i:s");
$author=$_SESSION["user_name"];
$str="INSERT INTO chat
(create_time,author,text) values('$time','$author','$text');";
mysql_query($str,$link_id);
mysql_close($link_id);
}
?>
<!--输入发言的表单-->
<form action="speak.php" method="post" target="_self">
<input type="text" name="text" cols="20">
<input type="submit" value="发言">
</form>
</body>
</html>
显示留言页面:
<!--chat_display.php: 显示留言页面-->
<html>
<head>
<title>显示用户留言 </title>
<meta http-equiv="refresh" content="5";
url=chat_display.php">
</head>
<body>
<?php
require_once("sys_conf.inc");
$link_id=mysql_connect($DBHOST,$DBUSER,$DBPWD);
mysql_select_db($DBNAME);
$str="select * from chat ORDER BY create_time;";
$result=mysql_query($str,$link_id);
$rows=mysql_num_rows($result);
@mysql_data_seek($result,$rows-15);
if($rows<15)
$l=$rows;
else
$l=15;
for($i=1;$i<=$l;$i++)
{
list($cid,$author,$create_time,$text)=mysql_fetch_row($result);
echo $create_time;
echo "";
echo"[".$author."]";
echo":";
echo $text;
echo"<BR>";
}
@mysql_data_seek($result,$rows-20);
list($limitime)=mysql_fetch_row($result);
$str="DELETE FROM chat WHERE create_time<'$limitime';";
$result=mysql_query($str,$link_id);
mysql_close($link_id);
?>
</body>
</html>
在调试过程中总是出现NOTICE错误,于是在php.ini里删除掉error_reporting =E_ALL
并将error_reporting = E_ALL & ~E_NOTICE前的逗号去掉了,重启服务器,终于实现了,今天真是高兴啊!