PHP留言板 PHP在线咨询留言系统

本文详细介绍本人最近做的一个PHP留言板(PHP在线咨询留言系统)

主要功能:
 留言,回复,注册,管理。

技术细节:
 1.注册与登录页面无刷新(AJAX)验证;
 2.一条留言下可显示多条回复(一个原创的算法);
 3.留言分页显示;4.留言管理(删除);
 5.页面布局(HTML+DIV+CSS);
 6.PHP操作MySQL数据库。

要使用此留言板,必须先创建数据库,而且MySQL账号设置为root,密码为root(或者自行修改多个php文件中默认的账号和密码)。数据库弄好之后,把“留言板-zollty”文件夹放到网站目录下,访问http://localhost/mb-zollty/index.php即可。

为方便大家使用,下面给出一个数据库备份文件guestbook.txt,你只需要恢复该数据库即可,恢复方式:
【在DOS下,输入: mysql -u用户名 -p密码 guestbook <F:\guestbook.txt(备份文件所在位置)】

guestbook.txt内容如下:

-- MySQL dump 10.11

--

-- Host: localhost    Database: guestbook

-- ------------------------------------------------------

-- Server version	5.0.51b-community-nt-log



/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;

/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;

/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;

/*!40101 SET NAMES utf8 */;

/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;

/*!40103 SET TIME_ZONE='+00:00' */;

/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;

/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;

/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;

/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;



--

-- Table structure for table `admin`

--



DROP TABLE IF EXISTS `admin`;

SET @saved_cs_client     = @@character_set_client;

SET character_set_client = utf8;

CREATE TABLE `admin` (

  `name` varchar(16) NOT NULL COMMENT '管理名称',

  `pwd` varchar(32) NOT NULL COMMENT '管理员密码',

  PRIMARY KEY  (`name`)

) ENGINE=MyISAM DEFAULT CHARSET=utf8;

SET character_set_client = @saved_cs_client;



--

-- Dumping data for table `admin`

--



LOCK TABLES `admin` WRITE;

/*!40000 ALTER TABLE `admin` DISABLE KEYS */;

INSERT INTO `admin` VALUES ('zollty','e10adc3949ba59abbe56e057f20f883e'),('harlizou','e10adc3949ba59abbe56e057f20f883e');

/*!40000 ALTER TABLE `admin` ENABLE KEYS */;

UNLOCK TABLES;



--

-- Table structure for table `guest`

--



DROP TABLE IF EXISTS `guest`;

SET @saved_cs_client     = @@character_set_client;

SET character_set_client = utf8;

CREATE TABLE `guest` (

  `id` int(11) NOT NULL auto_increment COMMENT '客户自增id',

  `name` varchar(16) NOT NULL COMMENT '客户名称',

  `email` varchar(60) default NULL COMMENT '电子邮箱',

  `qq` varchar(11) default NULL COMMENT 'QQ号码',

  PRIMARY KEY  (`id`)

) ENGINE=MyISAM AUTO_INCREMENT=7 DEFAULT CHARSET=utf8;

SET character_set_client = @saved_cs_client;



--

-- Dumping data for table `guest`

--



LOCK TABLES `guest` WRITE;

/*!40000 ALTER TABLE `guest` DISABLE KEYS */;

INSERT INTO `guest` VALUES (1,'test_01','[email protected]','112345678'),(2,'test_02','[email protected]','23658954'),(3,'test_03','[email protected]','54545454'),(6,'student_01','[email protected]','245457875'),(5,'四川大学','[email protected]','12227445');

/*!40000 ALTER TABLE `guest` ENABLE KEYS */;

UNLOCK TABLES;



--

-- Table structure for table `post`

--



DROP TABLE IF EXISTS `post`;

SET @saved_cs_client     = @@character_set_client;

SET character_set_client = utf8;

CREATE TABLE `post` (

  `id` int(11) NOT NULL auto_increment COMMENT 'POST主键自增id',

  `guest_id` varchar(11) NOT NULL COMMENT '发布人ID',

  `post` text NOT NULL COMMENT '发布内容',

  `post_time` varchar(14) NOT NULL COMMENT '发布时间',

  PRIMARY KEY  (`id`)

) ENGINE=MyISAM AUTO_INCREMENT=7 DEFAULT CHARSET=utf8;

SET character_set_client = @saved_cs_client;



--

-- Dumping data for table `post`

--



LOCK TABLES `post` WRITE;

/*!40000 ALTER TABLE `post` DISABLE KEYS */;

INSERT INTO `post` VALUES (1,'1','test_01test_01test_01test_01\r\ntest_01test_01test_01','1310744360'),(2,'2','您好!请问贵校今年的招生人数是多少?','1310744438'),(3,'3','请问,你们公司为实习生提供住宿吗?','1310744711'),(6,'6','请问,我们需要自己带电脑过去吗?','1310973804'),(5,'5','请问你们招收计算机专业的研究生吗?','1310744925');

/*!40000 ALTER TABLE `post` ENABLE KEYS */;

UNLOCK TABLES;



--

-- Table structure for table `revert`

--



DROP TABLE IF EXISTS `revert`;

SET @saved_cs_client     = @@character_set_client;

SET character_set_client = utf8;

CREATE TABLE `revert` (

  `id` int(11) NOT NULL auto_increment COMMENT 'revert主键自增id',

  `post_id` varchar(11) NOT NULL COMMENT '留言ID',

  `revert` text NOT NULL COMMENT '回复内容',

  `revert_time` varchar(14) NOT NULL COMMENT '回复时间',

  `admin` varchar(16) NOT NULL COMMENT '管理员名称',

  PRIMARY KEY  (`id`)

) ENGINE=MyISAM AUTO_INCREMENT=12 DEFAULT CHARSET=utf8;

SET character_set_client = @saved_cs_client;



--

-- Dumping data for table `revert`

--



LOCK TABLES `revert` WRITE;

/*!40000 ALTER TABLE `revert` DISABLE KEYS */;

INSERT INTO `revert` VALUES (1,'5 ','欢迎报考耶鲁大学计算机专业的研究生!','1310745105','zollty'),(11,'6 ','公司有电脑,但是宿舍没有哦!','1310973862','zollty'),(3,'3 ','提供住宿,实习生是每个月3500的工资!','1310745228','harlizou'),(4,'5 ','多条回复查询!','1310953006','zollty'),(5,'5 ','再次回复!!!!!!!!','1310959821','zollty'),(6,'2 ','test,sdgdskkjfdsgjkfjk,fdjfd,dfjkj.','1310972183','zollty'),(7,'2 ','什么情况??\r\ntest!','1310972216','zollty'),(10,'6 ','嗯','1310973819','harlizou'),(9,'5 ','换个号回复!','1310973648','harlizou');

/*!40000 ALTER TABLE `revert` ENABLE KEYS */;

UNLOCK TABLES;

/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;



/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;

/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;

/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;

/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;

/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;



-- Dump completed on 2011-07-18  8:00:13


也可以自行创建该数据库,MySQL下创建数据库的命令如下:
创建数据库:
create database guestbook default character set utf8 collate utf8_general_ci;
创建表:
guest——存储用户信息
post——存储留言
revert——存储回复信息
admin——管理员账号数据库
------------------------------------------------------
create table guest(
id int(11) not null auto_increment comment'客户自增id',
name varchar(16) not null comment'客户名称',
email varchar(60) null comment'电子邮箱',
qq varchar(11) null comment'QQ号码',
primary key(id)
)engine=Myisam;
-------------------------------------------------------
----------------------------------------------------
create table post(
id int(11) not null auto_increment comment'POST主键自增id',
guest_id varchar(11) not null comment'发布人ID',
post text not null comment'发布内容',
post_time varchar(14) not null comment'发布时间',
primary key(id)
)engine=Myisam;
------------------------------------------------------
---------------------------------------------------------
create table revert(
id int(11) not null auto_increment comment'revert主键自增id',
post_id varchar(11) not null comment'留言ID',
revert text not null comment'回复内容',
revert_time varchar(14) not null comment'回复时间',
admin varchar(16) null comment'管理名称',
primary key(id)
)engine=Myisam;
----------------------------------------------------------
-------------------------------------------------------
create table admin(
name varchar(16) not null comment'管理名称',
pwd varchar(16) not null comment'管理员密码',
primary key(name)
)engine=Myisam;
-------------------------------------------------------

外键关系是:guest.id = post.guest_id,post.id = revert.post_id,revert.admin=admin.name

 

文件清单:

index.php

login.php

logout.php

register.php

register_ok.php

chkadmin.php

post.php

revert.php

delete.php

conn.php

yzm.php

数据库连接文件conn.php如下:

<?php

	$conn=mysql_connect('localhost','root','root') or die("connection error".mysql_error());

	mysql_select_db("guestbook",$conn);

	mysql_query("set names gb2312");

?>

注册页面文件register.php如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />

<title>管理员注册</title>



</head>

<script language="javascript">

function rechk()

{

	if(register.name.value=="")

	{

		alert("用户名不能为空!");

		register.name.focus();

		return false;

	}

	if(register.pwd.value=="")

	{

		alert("用户密码不能为空!");

		register.pwd.focus();

		return false;

	}

}

function chkname(form)//利用Js的innerHTML方法实现无刷新验证,下同

{

	if(form.name.value=="")

	{

		name1.innerHTML="<font color=#FF0000>请输入用户名!</font>";  

	}

	else if(form.name.value.length>10)

	{

		name1.innerHTML="<font color=#FF0000>用户名长度应小于10位!</font>";  

	}

	else

	{

		name1.innerHTML="<font color=green>恭喜你,该用户名未被占用!</font>";  

	}

}

function chkpwd1(form)

{

	if(form.pwd.value=="")

	{

		pwd11.innerHTML="<font color=#FF0000>密码格式错误!</font>";  

	}

	else if(form.pwd.value.length<6)

	{

		pwd11.innerHTML="<font color=#FF0000>注册密码长度应大于6位!</font>";  

	}

	else

	{

		pwd11.innerHTML="<font color=green>输入正确</font>";  

	}

}

function chkpwd2(form)

{

	if(form.pwd2.value=="")

	{

		pwd21.innerHTML="<font color=#FF0000>请输入确认密码!</font>";  

	}

	else if(form.pwd2.value.length<6)

	{

		pwd21.innerHTML="<font color=#FF0000>确认密码长度应大于6位!</font>";  

	}

	else if(form.pwd.value!=form.pwd2.value)

	{

		pwd21.innerHTML="<font color=#FF0000>注册密码与确认密码不同!</font>"; 

	}

	else

	{

		pwd21.innerHTML="<font color=green>输入正确</font>";  

	}

}

function chkyzm(form)

{

	if(form.yzm.value=="")

	{

		yzm1.innerHTML="<font color=#FF0000>请输入效验码!</font>"; 

	}

	else if(form.yzm.value!=form.yzm2.value)

	{

		yzm1.innerHTML="<font color=#FF0000>效验码输入错误!</font>";

	}

	else

	{

		yzm1.innerHTML="<font color=green>输入正确</font>";

	}

}

function yzm(form)//利用yzm生成验证码图像

{

	var num1=Math.round(Math.random()*10000000);

	var num=num1.toString().substr(0,4);

	document.write("<img name=codeimg src='yzm.php?num="+num+"'>");

	form.yzm2.value=num;

}

function code(form)

{

	var num1=Math.round(Math.random()*10000000);

	var num=num1.toString().substr(0,4);

	document.codeimg.src="yzm.php?num="+num;

	form.yzm2.value=num;

}

</script>

</head>

<body>

<table width="450" border="0" align="center" cellpadding="0" cellspacing="0">

 <form id="register" name="register" method="post" action="register_ok.php">

 	<tr>

    	<td colspan="5" align="center" valign="middle"><h2>管理员注册</h2></td>

    </tr>

    <tr>

      <td width="81" height="25"><div align="right">用户名:</div></td>

      <td height="25" colspan="3"> 

          <input id="name" name="name" type="text"  onBlur="javascript:chkname(register)" onMouseOver="this.style.backgroundColor='#ffffff'" onMouseOut="this.style.backgroundColor='#e8f4ff'" /> <font color="red">*</font></td><!--单元格随鼠标变色-->

          <td height="25"><div id="name1"><font color="#999999">请输入用户名</font></div></td>

    </tr>

    

    <tr>

      <td width="81" height="25"><div align="right">注册密码:</div></td>

      <td height="25" colspan="3"> 

          <input id="pwd" name="pwd" type="password" onBlur="javascript:chkpwd1(register)" onMouseOver="this.style.backgroundColor='#ffffff'" onMouseOut="this.style.backgroundColor='#e8f4ff'"/> <font color="red">*</font></td>

      <td width="152"><div id="pwd11"><font color="#999999">请输入密码</font></div></td>

    </tr>

    

    <tr>

      <td width="81" height="25"><div align="right">确认密码:</div></td>

      <td height="25" colspan="3"> 

          <input id="pwd2" name="pwd2" type="password" onBlur=" javascript:chkpwd2(register)" onMouseOver="this.style.backgroundColor='#ffffff'" onMouseOut="this.style.backgroundColor='#e8f4ff'"/> <font color="red">*</font></td>

      <td height="25"><div id="pwd21"><font color="#999999">确认密码</font></div></td>

    </tr>

    

    <tr>

      <td height="25"><div align="right">验证码:</div></td>

      <td height="25"> 

        <input id="yzm" type="text" name="yzm" size="8" onBlur="javascript:chkyzm(register)" onMouseOver="this.style.backgroundColor='#ffffff'" onMouseOut="this.style.backgroundColor='#e8f4ff'"/>

      <input name="yzm2" type="hidden" value="" /></td>

      <td width="65" align="center" valign="middle"><script>yzm(register)</script></td>

      <td width="51"><a href="javascript:code(register)">看不清</a></td>

      <td height="25"><div id="yzm1"><font color="#999999">输入验证码</font></div></td>

    </tr>

    <tr>

      <td height="25" colspan="2"> 

          <input type="submit" name="Submit" value="提交" onclick="return rechk();"/>

          

        <input type="reset" value="重写" /></td>

      <td height="25" colspan="3"><div style="color:#FF0000">带"*"号的为必填项</div></td>

    </tr>

  </form>

</table>

</body>

</html>

管理员登录文件login.php如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />

<title>管理员登录</title>

</head>

<body>

<table width="590">

<tr>

<td width="582">

<form action="chkadmin.php" method="POST" name="myform">

管理员:<input type="text" name="username" size="20"/>

密码:<input type="password" name="password" size="20">

<input type="submit" value="登录" name="Submit"/>

<input type="button" onclick="javascript:location.href='index.php'" value="放弃"/>

<a style="color:blue" href="register.php">   注册</a>

</form>

</td>

</tr>

</table>

</body>

</html>

管理员回复文件revert.php如下:

<?php

session_start();

$con=mysql_connect('localhost','root','root') or die('链接数据库失败!');

mysql_query('set names gb2312');

mysql_select_db('GuestBook');



if(!
{1}

SESSION['login']){echo "<script>alert('没有登录不能回复!');location.href='index.php';</script>";exit();}if(
{1}

POST['Submit']){if(!get_magic_quotes_gpc()){//判断PHP解析器是否为数据加上了自动转义,如果没有,就执行foreach语句自己添加转义foreach (
{1}

POST as $items){$items = addslashes($items);}}if(strlen(
{1}

POST['revert'])>400){echo "<script>alert('回复内容过长!');history.go(-1);</script>";exit();}$post_id =
{1}

POST['post_id'];$revert =
{1}

POST['revert'];$time = time();//获取当前时间$insertRevertSql = "insert into revert (post_id,revert,revert_time,admin) value('$post_id','$revert','$time','
{1}

SESSION[admin_name]')";//将当前的回复信息存入数据库if(mysql_query($insertRevertSql)){echo "<script>alert('回复成功\'$time\'');location.href='index.php';</script>";exit();}else{echo "<script>alert('回复失败!');history.go(-1);</script>";}}?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312" /><title>无标题文档</title></head><body><table><tr><td><form action="revert.php" method="POST" name="form1">回复内容:<textarea name="revert" cols="30" rows="5" id="revert"></textarea><input type="hidden" name="post_id" value="<?php echo
{1}

GET['id']?> "size="20"><input type="submit" value="回 复" name="Submit"/><input type="button" onclick="javascript:history.go(-1);" value="放弃"/></form></td></tr></table></body></html>
 
  

登出文件logout.php如下:

<?php

session_start();
{1}

SESSION['login']=false;//置为false,取消管理员权限echo "<script language='javascript'>location.href='index.php';</script>";?>
 
  

发送留言文件post.php如下:

<?php

header('content-type:text/html;charset=utf-8');//告诉程序我们的脚本将返回什么格式的数据,和返回数据的字符集,否则IE里面看到返回是乱码

if(!get_magic_quotes_gpc())

{//判断PHP解析器是否为数据加上了自动转义,如果没有,就执行foreach语句自己添加转义

	foreach (
{1}

POST as &$items){$items = addslashes($items);}}$name =
{1}

POST['name'];$qq =
{1}

POST['qq'];$email =
{1}

POST['email'];$post =
{1}

POST['post'];if($name==""||strlen($name)>10){//通过界定符“<<<”在PHP代码中嵌入Js代码和其他代码echo <<<tem<script language="javascript">alert('请输入正确的有户名');history.go(-1);</script>tem;exit();}if($qq==""&&$email==""){echo <<<tem<script>alert('Email和QQ必须输入一个!');history.go(-1);</script>tem;exit();}if($qq!=""&&(!is_numeric($qq)||$qq>9999999999||$qq<=9999)){echo <<<tem<script>alert("请输入正确的QQ号码");history.go(-1);</script>tem;exit();}if($email!=""&&(!ereg("^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+",$email)||strlen($email)>60)){echo <<<tem<script>alert("请输入正确的Email");history.go(-1);</script>tem;exit();}if(strlen($post)>400){echo <<<tem<script>alert("输入的留言内容太长!");history.go(-1);</script>tem;exit();}//链接数据库$con=mysql_connect('localhost','root','root') or die('链接数据库失败!'.mysql_error());mysql_query('set names gb2312');mysql_select_db('GuestBook');//把客户信息插入guest表$insertSql="insert into guest (name,qq,email) values ('$name','$qq','$email')";if(mysql_query($insertSql)){$guestid = mysql_insert_id();}else{echo $insertSql;echo mysql_error();echo "数据插入失败!";exit();}//把以上插入取得的客户id和留言信息插入到post表中$post_time = time();$insertPostSql = "insert into post(guest_id,post,post_time) values('$guestid','$post','$post_time')";if(mysql_query($insertPostSql)){echo <<<tem<script>alert("留言成功");location.href="index.php";</script>tem;}else{echo <<<tem<script>alert("留言失败");location.href="index.php";</script>tem;}?>
 
  

注册验证文件register_ok.php如下:

<?php

session_start();

class chkinput{

	var $name;

	var $pwd;

	function chkinput($x,$y)

	{

		$this->name=$x;//实例化类

		$this->pwd=$y;

	}

	function checkinput()

	{

		include "conn.php";//引进数据库连接文件

		$return=mysql_query("insert into admin(name,pwd) values('".$this->name."','".$this->pwd."')");//将注册的账号和密码插入数据库

		if($return)

		{
{1}

SESSION['login']=true;//注册成功后,赋予管理员权限
{1}

SESSION[admin_name]=
{1}

POST[name];//记录登录的管理员的nameecho "<script language='javascript'>alert('恭喜你,注册成功!');location.href='index.php';</script>";}else{echo "<script language='javascript'>alert('管理员注册失败!');history.back();</script>";exit();}}}$obj=new chkinput(trim(
{1}

POST[name]),trim(md5(
{1}

POST[pwd])));//注册的密码通过MD5加密后在存入数据库$obj->checkinput();?>
 
  

管理员登录验证文件chkadmin.php如下:

<?php

session_start();

class chkinput

{

	var $name;

	var $pwd;

	function chkinput($x,$y)

	{

		$this->name=$x;

		$this->pwd=$y;

	}

	function checkinput()

	{

		include "conn.php";

		$sql=mysql_query("select * from admin where name='".$this->name."'");//即执行select * from admin where name='传入的数据'

		$info=mysql_fetch_array($sql);

		if($info==fasle)

		{

			echo "<script language='javascript'>alert('不存在此管理员!');history.back();</script>";

			exit();

		}

		else

		{

			if($info[pwd]==$this->pwd)

			{
{1}

SESSION['login']=true;
{1}

SESSION[admin_name]=$info[name];echo "<script language='javascript'>alert('恭喜你,登录成功!');location.href='index.php';</script>";}else{echo "<script language='javascript'>alert('密码错误!');history.back();</script>";exit();}}}}$obj=new chkinput(trim(
{1}

POST['username']),trim(md5(
{1}

POST['password'])));$obj->checkinput();?>
 
  

生成随机文件yzm.php如下:

<?php

//此为生成随即验证码的文件,主要使用的是srand()和imagestring()函数

srand((double)microtime()*1000000);

$im=imagecreate(42,16);

$black=imagecolorallocate($im,0,0,0);

$white=imagecolorallocate($im,255,255,255);

$gray=imagecolorallocate($im,200,200,200);

imagefill($im,0,0,$gray);



for($i=0;$i<4;$i++){

 $str=mt_rand(1,3);

 $size=mt_rand(3,6);

 $authnum=substr(
{1}

GET[num],$i,1); imagestring($im,$size,(2+$i*10),$str,$authnum,imagecolorallocate($im,rand(0,130),rand(0,130),rand(0,130)));} for($i=0;$i<200;$i++){ $randcolor=imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255)); imagesetpixel($im,rand()%70,rand()%30,$randcolor); }imagepng($im);imagedestroy($im);?>
 
  

删除留言页面文件delete.php如下:

<?php

session_start();

header('content-type:text/html;charset=utf-8');

$con=mysql_connect('localhost','root','root') or die('链接数据库失败!');

mysql_query('set names gb2312');

mysql_select_db('GuestBook');



if(!
{1}

SESSION['login'])//若管理员未登录,则拒绝,退出该文件{echo "<script>alert('权限不足!');window.location.href='index.php';</script>";exit();}if(isset(
{1}

GET['id'])&&
{1}

GET['id']!="")//如果post.id存在且不为空,则可执行删除操作{$delRevertSql="delete from revert where post_id=".
{1}

GET['id'];mysql_query($delRevertSql);$delGuestSql="delete from guest where id = (select guest_id from post where id=".
{1}

GET['id'].")";mysql_query($delGuestSql);$delPostSql="delete from post where id=".
{1}

GET['id'];mysql_query($delPostSql);if(mysql_error()==""){echo "<script>alert('删除成功!');window.location.href='index.php';</script>";}}?>
 
  

首页文件index.php如下:

<?php

session_start();

$con=mysql_connect('localhost','root','root') or die('链接数据库失败!');

mysql_query('set names gb2312');

mysql_select_db('GuestBook');



$pagesize = 8;//每一页显示多少留言记录

if(isset(
{1}

GET['page'])&&
{1}

GET['page']!='') $page=
{1}

GET['page'];else $page=0;$sql = "SELECT a . * , b.name, b.email, b.qq, c.revert_time, c.post_id, c.revert,c.adminFROM post aLEFT JOIN revert c ON ( a.id = c.post_id ) , guest bWHERE a.guest_id = b.idORDER BY a.id DESC";$numRecord = mysql_num_rows(mysql_query($sql));//获取总记录条数$totalpage = ceil($numRecord/$pagesize);//进1法取整$recordSql = $sql. " LIMIT ".$page*$pagesize.",".$pagesize;//附上限制条件$result = mysql_query($recordSql);?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312" /><title>简易留言板PHP</title><style type="text/css">body {margin-left: 0px;margin-top: 0px;}a:link {text-decoration: none;color: #FF6600;}.STYLE1 {color: #FFFFFF;font-weight: bold;font-size: 20px;}</style><script language="javascript">function checkInput(){//Js正则表达式验证var Email = document.getElementById('email');//通过id获取文本框中输入的内容var QQ = document.getElementById('qq');var name = document.getElementById('name');var post = document.getElementById('post');//验证用户名:不能超过10个字符(5个汉字),不能输入非法字符,不能为空nameValue = name.value.replace(/\s+/g,"");//replace()方法用于替换一个与正则表达式匹配的子串,/\s+/后跟g代表全局替换var SPECIAL_STR = "~!%^&*();\"?><[]{}\\|,:/=+—";var nameflag=true;for(i=0;i<nameValue.lenght;i++){if (SPECIAL_STR.indexOf(nameValue.charAt(i)) !=-1)//charAt()方法可返回指定位置的字符nameflag=false;//indexOf()方法可返回某个指定的字符串值在字符串中首次出现的位置}if(nameValue==''){alert('请填写用户名称!');return false;}if(nameValue.length>10){alert('用户名称最多10个字符(5个汉字)!');return false;}if(nameflag===false){alert('用户名称不能包含非法字符请更改!');return false;}//验证QQ号码var par =/^[1-9]\d{4,12}$/;if(QQ.value!=''&&!par.test(QQ.value)){alert('请输入正确的QQ号码');return false;}//验证Email地址var emailpar = /^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/;if(Email.value!=''&&!emailpar.test(Email.value)){alert('请输入正确的邮箱地址!');return false;}if(QQ.value==''&&Email.value==''){alert('邮箱和QQ必选其一');return false;}if(post.value==""){alert('请输入留言内容!');return false;}if(post.value.length>400){alert('留言内容太长!');return false;}}</script></head><table width="800" border="0" align="center"> <tr> <td height="80" bgcolor="#003366"><span class="STYLE1"> 简易留言板PHP(<a href="http://blog.csdn.net/zollty" class="STYLE1">zollty</a>)</span></td> </tr> <tr> <td height="5" bgcolor="#efefef"></td> </tr></table><table width="800" border="0" align="center" bgcolor="#fefefe"><?php$reply_id=0;//记录留言idwhile($rs=mysql_fetch_object($result))//把返回对象赋值给rs变量{if($rs->id != $reply_id)//若留言id不等于当前回复的id则显示回复信息,否则隐藏留言,直接显示后面的回复{?> <tr><hr/></tr><tr> <div style="color:blue">留言人:<?php echo $rs->name?> | Email:<?php echo $rs->email?> | QQ:<?php echo $rs->qq?> | 留言时间:<?php echo date("Y-m-d H:i:s",$rs->post_time+8*3600)?></div></tr> <?php if(isset(
{1}

SESSION['login'])&&
{1}

SESSION['login'])//如果管理员已经登录,则显示回复和删除的权限 { ?> <tr> <a href="revert.php?id=<?php echo $rs->id?>">回复</a> | <a href="delete.php?id=<?php echo $rs->id?>">删除</a> </tr> <?php }//end if(isset…) ?> <?php echo nl2br(htmlspecialchars($rs->post))."<br/>"; }//end if($rs->id…) ?> <?phpif($rs->revert!=""){?> <br/><font style="color:red">管理员[<?php if($rs->admin != "") echo $rs->admin;?>]回复:</font><?php echo nl2br(htmlspecialchars($rs->revert));?>[回复时间:<?php if($rs->revert_time!="") echo date("Y-m-d H:i:s",$rs->revert_time+8*3600);?>]<?php }//end if($rs->revert!="")?><?php$reply_id=$rs->id;//把post.id赋值给$reply_id以便下一轮检测post.id是否与上一轮相同}//end while()?></table><table width="800" border="0" align="center" bgcolor="#B1C3D9"> <tr> <td ><?php if($page>0) echo "<a href='index.php?page=".($page-1)."'>上一页|</a>" ;if($page<$totalpage-1) echo "<a href='index.php?page=".($page+1)."'>下一页</a>" ;//点击下一页后page变量加1,将显示"上一页"?></td> </tr></table><form action="post.php" method="post" id="postForm" name="postForm"><table width="800" border="0" align="center" cellspacing="1" bgcolor="#efefef"> <tr> <td width="117" bgcolor="#FFFFFF">姓名:</td> <td width="673" bgcolor="#FFFFFF"><label> <input type="text" name="name" id="name" /> </label></td> </tr> <tr> <td bgcolor="#FFFFFF">Email:</td> <td bgcolor="#FFFFFF"><label> <input type="text" name="email" id="email" /> </label></td> </tr> <tr> <td bgcolor="#FFFFFF">QQ:</td> <td bgcolor="#FFFFFF"><label> <input type="text" name="qq" id="qq"/> </label></td> </tr> <tr> <td colspan="2" bgcolor="#FFFFFF">留言内容:</td> </tr> <tr> <td colspan="2" bgcolor="#FFFFFF"><label> <textarea name="post" id="post" cols="40" rows="5"></textarea> </label></td> </tr> <tr> <td colspan="2" bgcolor="#FFFFFF"><label> <input type="submit" name="Submit" value="提交" onclick="return checkInput();"/>           <input type="reset" name="Submit2" value="重置" /> </label> <a href="login.php">管理员登录</a> <?php if(isset(
{1}

SESSION['login'])&&
{1}

SESSION['login']) { ?> <a href="logout.php">退出</a></td> <?php }//end if ?> </tr></table></form></body></html>
 
  

 

 

 

乱码(部分换行符没了,部分代码丢失(换成了“{1}”),某些一段代码变成了几段),编辑器的问题,再次强调:是文本编辑器的问题,我的代码是纯txt格式,操作也无错,我试过了几次。

 

找到乱码的原因了:http://blog.csdn.net/zollty/article/details/7163049

 

你可能感兴趣的:(PHP)