PHP留言板

简单的PHP留言板制作。

一、功能:用户查看留言、发表留言,管理员登录、回复、删除留言、编辑回复、删除回复、分页

二、PHP文件制作:
1、conn.php 数据库连接文件

2、reportMessage 发表留言页面

2、messageBoard.PHP   留言板主页面

3、login.php 管理员登录页面

三、数据库messageboard

1、留言表message

字段:uid(主键id),username(用户名),title(留言标题),content(留言内容),reply(管理员回复),messageTime(留言时间)

2、管理员表admin

字段:aid(主键id),username(用户名),password(密码)

四、其他注意事项

1、留言内容过滤HTML字符,转换回车和空格

2、防刷新重复提交

五、创建数据库

drop database if exists messageboard;
create database messageboard character set utf8 collate utf8_general_ci;
use messageboard;

DROP TABLE IF EXISTS `message`;
CREATE TABLE `message` (
  `mid` int(11) NOT NULL  auto_increment,
  `username` char(20) NOT NULL,
`title` VARCHAR(50) NOT NULL,
  `content` text NOT NULL,
  `reply` text,
  `messagetime` int(11) NOT NULL,
  PRIMARY KEY  (`uid`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

DROP TABLE IF EXISTS `admin`;
CREATE TABLE `admin` (
  `aid` int(11) NOT NULL  auto_increment,
  `username` char(20) NOT NULL,
  `password` char(50) NOT NULL,
  PRIMARY KEY  (`aid`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
-- 预设管理员,密码为admin
insert into admin values(null,'admin','21232f297a57a5a743894a0e4a801fc3 ');


六、页面代码

1、样式表文件 main.css

@CHARSET "UTF-8";
table.mTable{
	width:600px;
	margin:0 auto;
	text-align:left;
    font-family: verdana,arial,sans-serif;
    font-size:12px;
    color:#333333;
    border-width: 1px;
    border-color: #666666;
    border-collapse: collapse;
}
table.mTable th {
    border-width: 1px;
    padding: 8px;
    border-style: solid;
    border-color: #666666;
    background-color: #dedede;
}
table.mTable td {
    border-width: 1px;
    padding: 8px;
    border-style: solid;
    border-color: #666666;
    background-color: #ffffff;
}
table.lTable{
	width:300px;
}
.btnTd{
	text-align:center;
}
.mainLeftTd{
	width:120px;
}
.manageMsg{
	float:right;
}
.manageMsg a{
	text-decoration:none;
}
replyTd{
	color:009900;
	background-color:#0FF;
}


2、数据库连接文件 conn.php


<?php
/*
 * Created on 2014-11-1
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
//连接数据库
$conn = @ mysql_connect("localhost:3306", 'root', 'root') or die("数据库服务器连接失败!");
mysql_select_db("messageboard") or die("数据库连接失败");
mysql_set_charset("utf8"); //设置当前连接的默认字符集。
?>

3、发表留言文件 reportMessage.php

<?php
/*
 * Created on 2014-11-1
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
require_once("conn.php");
if($_POST and array_key_exists("username",$_POST)){
	$sql = "insert into message (username,title,content,messagetime) values('$_POST[username]','$_POST[title]','$_POST[content]',".time().")";
	$result = mysql_query($sql) or die("SQL语句出错,请检查SQL语句!");
	echo "<script>alert('恭喜你,留言成功!');window.location.href='messageBoard.php';</script>";
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>发表留言</title>
<link rel="stylesheet" href="main.css" type="text/css" />
<script type="text/javascript">
function check(){
	if(document.mForm.username.value == ""){
		alert("用户名不能为空!");
		return false;
	}else if(document.mForm.title.value == ""){
		alert("留言标题不能为空!");
		return false;
	}else if(document.mForm.content.value == ""){
		alert("留言内容不能为空!");
		return false;
	}
}
</script>
</head>
<body>
<table class="mTable">
<caption>发表留言</caption>
<form name="mForm" action="" method="post" onSubmit="return check();">
<tr><td>用户名:</td><td><input type="text" name="username" value="" /></td></tr>
<tr><td>留言标题:</td><td><input type="text" name="title" value="" size="50" /></td></tr>
<tr><td>留言内容:</td><td><textarea name="content" cols="50" rows="8"></textarea></td></tr>
<tr><td colspan="2" class="btnTd"><input type="submit" name="mBtn" value="发 表" /> <input type="button" name="returnBtn" value="返 回" οnclick="javascript:window.location.href='messageBoard.php';" /></td></tr>
</form>
</table>
</body>
</html>

4、留言板主页面 messageBoard.php

<?php
/*
 * Created on 2014-11-1
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
//一、功能:用户查看留言、发表留言,管理员登录、回复、删除留言、编辑自己回复的内容
require_once("conn.php");
//总记录数
$totalRecord = count(getData("select * from message"));
//当前页
if (isset($_GET["page"]) and (int) $_GET["page"] >= 1) {
	$currentPage = $_GET["page"] ? (int) $_GET["page"] : 1;
} else {
	$currentPage = 1;
}
//每页记录数
$pageSize = 3;
//总页数
$totalPage = ceil($totalRecord / $pageSize);
//组装获取当前页记录的SQL语句
$sql = "select * from message limit " . ($currentPage -1) * $pageSize . "," . $pageSize;
//存储当前页记录的数组
$rows = getData($sql);
//当前页的路径=相对根目录路径+文件名
$url = $_SERVER['PHP_SELF'];

//判断管理员是否已登录
$isLogin = false;
if(isset($_COOKIE["username"])) $isLogin = true;
if($isLogin){
	//回复留言
	if(isset($_POST["reply"])){
		$sql = "update message set reply='".$_POST["reply"]."' where mid=".(int)$_POST["mid"];
		$result = mysql_query($sql) or die("SQL语句出错,请检查SQL语句!");
		echo "<script>alert('回复成功!');window.location.href='messageBoard.php';</script>";
	}
	//删除留言
	if(isset($_GET["act"]) and ($_GET["act"] == "del")){
		$sql = "delete from message where mid=".(int)$_GET["mid"];
		$result = mysql_query($sql) or die("SQL语句出错,请检查SQL语句!");
		echo "<script>alert('删除成功!');window.location.href='messageBoard.php';</script>";
	}
	//删除回复
	if(isset($_GET["act"]) and ($_GET["act"] == "delReply")){
		$sql = "update message set reply='' where mid=".(int)$_GET["mid"];
		$result = mysql_query($sql) or die("SQL语句出错,请检查SQL语句!");
		echo "<script>alert('删除回复成功!');window.location.href='messageBoard.php';</script>";
	}
}
//获取数据库中的数据
function getData($sql) {
	$result = @ mysql_query($sql) or die("SQL语句出错,请检查SQL语句!");
	$rows = array ();
	while ($row = mysql_fetch_array($result)) {
		$rows[] = $row;
	}
	return $rows;
}
//过滤HTML标记,转换空格和回车
function changeStr($str){
	$str = htmlspecialchars($str);
	$str = str_replace("\n","<br />",str_replace(" "," ",$str));
	return $str;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>PHP留言板</title>
<link rel="stylesheet" href="main.css" type="text/css" />
<script type="text/javascript">
function check(o){
	if(o.reply.value == ""){
		alert("回复内容不能为空!");
		return false;
	}
}
function showAddReply(addReplyTrId){
	var allAddReplyTrArr = getClass("tr","addReplyTr");
	for(var i=0;i<allAddReplyTrArr.length;i++){
		allAddReplyTrArr[i].style.display = "none";
	}
	document.getElementById(addReplyTrId).style.display = "table-row";
	var allEditReplyTrArr = getClass("tr","editReplyTr");			//所有的编辑回复窗口要隐藏
	for(var i=0;i<allEditReplyTrArr.length;i++){
		allEditReplyTrArr[i].style.display = "none";
	}
	var allReplyTrArr = getClass("tr","replyTr");					//所有有内容的回复窗口要显示
	for(var i=0;i<allReplyTrArr.length;i++){
		allReplyTrArr[i].style.display = "table-row";
	}
}
function showEditReply(editReplyTrId,replyTrId){
	var allEditReplyTrArr = getClass("tr","editReplyTr");
	for(var i=0;i<allEditReplyTrArr.length;i++){
		allEditReplyTrArr[i].style.display = "none";
	}
	document.getElementById(editReplyTrId).style.display = "table-row";
	var allReplyTrArr = getClass("tr","replyTr");
	for(var i=0;i<allReplyTrArr.length;i++){
		allReplyTrArr[i].style.display = "table-row";
	}
	document.getElementById(replyTrId).style.display = "none";
	var allAddReplyTrArr = getClass("tr","addReplyTr");			//所有的回复窗口也要隐藏
	for(var i=0;i<allAddReplyTrArr.length;i++){
		allAddReplyTrArr[i].style.display = "none";
	}
}
//这个方法是用来替代document.getElementsByClassName,因为该死的IE不支持document.getElementsByClassName,所以写了这样一个方法来替代。
function getClass(tagname,className) { 					//tagname指元素,className指class的值
	if (document.getElementsByClassName) {   			   //判断浏览器是否支持getElementsByClassName,如果支持就直接的用
		return document.getElementsByClassName(className);
	}else{														//当浏览器不支持getElementsByClassName的时候用下面的方法
		var tagname = document.getElementsByTagName(tagname);  //获取指定元素
		var tagnameAll = [];     							//这个数组用于存储所有符合条件的元素
		for (var i = 0; i < tagname.length; i++) {    		//遍历获得的元素
			if (tagname[i].className == className) {     	//如果获得的元素中的class的值等于指定的类名,就赋值给tagnameAll
				tagnameAll[tagnameAll.length] = tagname[i];
			}
		}
		return tagnameAll;
	}
}
//删除留言时的判断
function confirmDel(mid,url,act){
	if(confirm("确定要删除吗?")){
		window.location.href = url+"?mid="+mid+"&act="+act;
	}
}
</script>
</head>
<body>

<table class="mTable">
<tr><td colspan="2"><a href="reportMessage.php">我要留言</a> <?if(!$isLogin):?><a href="login.php">管理留言</a><?endif;?></td></tr>
<? foreach($rows as $row):?>
<tr><td class="mainLeftTd">用户名:<?=$row["username"]?></td><td>留言标题:<?=$row["title"]?><?if($isLogin):?><span class="manageMsg"><?if($row["reply"]==""):?><a href="#<?="replyTr".$row["mid"]?>" οnclick="showAddReply('<?="addReplyTr".$row["mid"]?>');">回复</a><?endif;?>  <a href="#" οnclick="confirmDel(<?=$row["mid"]?>,'<?=$url?>','del');">删除</a></span><?endif;?></td></tr>
<tr><td>留言内容:</td><td><?=changeStr($row["content"])?></td></tr>
<?if($row["reply"]!=""):?><tr class="replyTr" id="replyTr<?=$row["mid"]?>"><td>管理员回复:</td><td>RE  <span class="replyContent"><?=$row["username"]?>:<br /><?=changeStr($row["reply"])?></span><?if($isLogin):?><span class="manageMsg"><a href="#<?="editReplyTr".$row["mid"]?>" οnclick="showEditReply('<?="editReplyTr".$row["mid"]?>','<?="replyTr".$row["mid"]?>');">编辑</a>  <a href="#" οnclick="confirmDel(<?=$row["mid"]?>,'<?=$url?>','delReply');">删除</a></span><?endif;?></td></tr>
<tr class="editReplyTr" id="editReplyTr<?=$row["mid"]?>" style="display:none;"><form name="editForm<?=$row["mid"]?>" action="" method="post" onSubmit="return check(this);"><td>管理员编辑回复:</td><td><textarea name="reply" cols="40" rows="7"><?=changeStr($row["reply"])?></textarea> <input type="hidden" name="mid" value="<?=$row["mid"]?>" /><input type="submit" name="replyBtn" value="确认编辑" /></td></form></tr>
<?else:?>
<tr class="addReplyTr" id="addReplyTr<?=$row["mid"]?>" style="display:none;"><form name="addForm<?=$row["mid"]?>" action="" method="post" onSubmit="return check(this);"><td>管理员回复:</td><td><textarea name="reply" cols="40" rows="7"></textarea> <input type="hidden" name="mid" value="<?=$row["mid"]?>" /><input type="submit" name="replyBtn" value="确认回复" /></td></form></tr>
<?endif;?>
<tr class="separatorTr"><td colspan="2"></td></tr>
<? endforeach;?>
<!-- 分页 -->
<?if($totalRecord>$pageSize):?>
<tr><td colspan="3">
<? if ($currentPage == 1):?>
首页 上一页 <a href="<?=$url?>?page=<?=$currentPage+1?>">下一页</a> <a href="<?=$url?>?page=<?=$totalPage?>">尾页</a>
<?elseif($currentPage == $totalPage): ?>
<a href="<?=$url?>?page=1">首页</a> <a href="<?=$url?>?page=<?=$currentPage-1?>">上一页</a> 下一页 尾页
<?else:?>
<a href="<?=$url?>?page=1">首页</a> <a href="<?=$url?>?page=<?=$currentPage-1?>">上一页</a> <a href="<?=$url?>?page=<?=$currentPage+1?>">下一页</a> <a href="<?=$url?>?page=<?=$totalPage?>">尾页</a>
<?endif;?>
</td></tr>
<? endif; ?>
</table>
</body>
</html>

5、管理员登录页面 login.php

<?php
/*
 * Created on 2014-11-1
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
 require_once("conn.php");
if(isset($_COOKIE["username"])){
	echo "<script>window.location.href='messageBoard.php';</script>";
}else{
	if($_POST and array_key_exists("username",$_POST)){
		$sql = "select * from admin where username='".$_POST["username"]."' and password='".md5($_POST["password"])."'";
		$result = mysql_query($sql);
		if(mysql_num_rows($result) != 0){
			setCookie("username",$_POST["username"]);
		}else{
			echo "<script>alert('用户名或密码错误!请重新输入。');window.location.href='login.php';</script>";
		}
	}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>管理员登录</title>
<link rel="stylesheet" href="main.css" type="text/css" />
<script type="text/javascript">
function check(){
	if(document.mForm.username.value == ""){
		alert("用户名不能为空!");
		return false;
	}else if(document.mForm.password.value == ""){
		alert("密码不能为空!");
		return false;
	}
}
</script>
</head>
<body>
<table class="mTable lTable">
<caption>管理员登录</caption>
<form name="mForm" action="" method="post" onSubmit="return check();">
<tr><td>用户名:</td><td><input type="text" name="username" value="" /></td></tr>
<tr><td>密码:</td><td><input type="password" name="password" value="" /></td></tr>
<tr><td colspan="2" class="btnTd"><input type="submit" name="mBtn" value="登 录" /> <input type="button" name="returnBtn" value="返 回" οnclick="javascript:window.location.href='messageBoard.php';" /></td></tr>
</form>
</table>
</body>
</html>

完工!^_^


<html> <head> <title>留言板设计</title> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> <style type="text/css"> <!-- body,td,th { font-size: 14px; color: #003399; } .xs{ font-size:12px} --> </style></head> <body bgcolor="#FFFFFF" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0" background="images/pub_01.gif" style="background-repeat:repeat-x"> <!-- ImageReady Slices (留言板设计(参考)副本.jpg) --> <form name="form1" method="post" action="add.asp"> <table width="600" height="500" border="0" align="center" cellpadding="0" cellspacing="0" id="__01"> <tr> <td width="36" height="500" rowspan="6"> </td> <td height="35" colspan="4"> </td> <td width="50" height="500" rowspan="6"> </td> </tr> <tr> <td colspan="4"><img src="images/pub_04.gif" width="514" height="69" alt=""></td> </tr> <tr> <td width="19" rowspan="4"><img src="images/pub_05.gif" width="19" height="396" alt=""></td> <td colspan="3"><img src="images/pub_06.gif" width="495" height="22" alt=""></td> </tr> <tr> <td><img src="images/pub_07.gif" width="49" height="321" alt=""></td> <td width="317" height="321" background="images/2_02.gif"><table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0"> <tr> <td height="17" colspan="4"> </td> </tr> <tr> <td width="61" height="30">昵称:</td> <td width="235" height="30" colspan="2"><input type="text" name="nc" style="width:120px; height:25px; border:solid #000000 1px;"></td> <td width="21" height="30"> </td> </tr> <tr> <td width="61" height="30">标题:</td> <td height="30" colspan="2"><input type="text" name="bt" style="width:180px; height:25px; border:solid #000000 1px;"></td> <td height="30"> </td> </tr> <tr> <td height="135" valign="middle">内容:</td> <td height="135" colspan="2" valign="middle"><textarea name="nr" cols="30" rows="10" style="border:solid #000000 1px;"></textarea></td> <td height="135"> </td> </tr> <tr> <td height="30"> </td> <td height="30" align="center"><input type="image" name="imageField" src="images/2_05.gif">  <a href="#"><img src="images/2_07.gif" width="62" height="33" border="0"></a></td> <td align="center" class="xs"><a href="view.asp">显示留言</a></td> <td height="30"><a href="view.asp"></a></td> </tr> </table></td> <td><img src="images/pub_09.gif" width="129" height="321" alt=""></td> </tr> <tr> <td colspan="3"><img src="images/pub_10.gif" width="495" height="22" alt=""></td> </tr> <tr> <td height="31" colspan="3"> </td> </tr> </table> </form> <!-- End ImageReady Slices --> </body> </html> <script language="vbscript"> function biaodan() dim usernc,userbt,usernr usernc=form1.nc.value userbt= form1.bt.value usernr=form1.nr.value if len(trim(usernc))<6 then alert("你输入的昵称有误!") biaodan=false exit function else if userbt="" then alert("你输入的标题有误!") biaodan=false exit function else if usernr="" then alert ("你输入的内容有误!") biaodan=false exit function end if end if end if end function sub quxiao() form1.nc.value="" form1.bt.value="" form1.nr.value="" end sub </script>
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值