前言
本案例是明哥教学案例,但并没有把所有页面实现,只简单实现、注册、登录、发布留言、和展示留言页、缺少删除留言页面、管理员和普通用户区分的权限管理、密码修改页等等,本案例仅供参考。
效果图如下:
数据库:用户表
数据库:留言表
注册页面:
登录页面:
留言发布页:
留言展示页:
数据库代码:
-- 创建留言数据库,里面有两张表 admin 和Message
create database mg_board;
-- 创建 admin表,存放用户名和密码
create table admin(
username varchar(20) not null,
userpassword varchar(20) not null
);
-- 创建message表,记录留言id,留言人,留言日期,留言内容以及回复
create table message(
id int(4) not null auto_increment primary key,
author varchar(20) not null,
addtime datetime not null,
content varchar(1000) not null,
reply varchar(1000) not null
);
注册页代码:register.php
<?php
error_reporting(E_ALL&~E_NOTICE);
header("Content-Type: text/html;charset=utf-8");
date_default_timezone_set('PRC');
$link=mysqli_connect("127.0.0.1","root","root");
if ($link){
mysqli_select_db($link,"board");
if (isset($_POST["btn2"])){
$names=$_POST['names'];
$pwd=$_POST['pwd'];
if ($names==""||$pwd==""){
echo "<script>alert('用户名或密码不能为空');location.href='register.php';</script>";
exit();
}
$query="insert into admin(username,userpassword)values('$names','$pwd')";
$result=mysqli_query($link,$query);
echo "<script>alert('用户注册成功');location.href='login.php';</script>";
}
}
mysqli_close($link);
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>注册</title>
</head>
<body>
<table border=1 cellspacing=0 cellspadding=0 align=center width=400 height="358">
<caption>明哥在线留言系统</caption>
<tr>
<td align="center">
<a href=login.php>[用户登录]</a>
<a href=register.php>[用户注册]</a>
</td>
</tr>
<tr>
<td >
<form method="post" action="register.php">
<table border="1" width="100%" id="table1" cellspcing="0" cellpadding="0" >
<tr>
<td colspan="2" >
<p align="center">欢迎来到注册页面</p>
</td>
</tr>
<tr>
<td >
<p align="center">用户名</P>
</td>
<td >
<input type="text" name="names" size="20">
</td>
</tr>
<tr>
<td>
<p align="center">密 码</p>
</td>
<td>
<input type="password" name="pwd" size="20">
</td>
</tr>
<tr>
<td colspan="2">
<p align="center"><input type="submit" value="注册" name="btn2"></p>
</td>
</tr>
</table>
</form>
</td>
</tr>
<tr>
<td align=center>
<a href="https://blog.csdn.net/weixin_40845165">版权归明哥所有</a><br>
</font>
</td>
</tr>
</body>
</html>
登录页代码:login.php
<?php
error_reporting(E_ALL&~E_NOTICE);
header("Content-Type: text/html;charset=utf-8");
date_default_timezone_set('PRC');
$name = $_POST["name"];
if( $name != ""){
$password = $_POST['userpassword'];
$link = mysqli_connect("127.0.0.1","root","root");
mysqli_select_db($link,"board");
$query = "select * from admin where username = '$name'";
$result = mysqli_query($link,$query);
if( mysqli_num_rows($result) < 1){
echo "该用户不存在,请重新登录!<br>";
}else{
$info = mysqli_fetch_array($result);
if( $info['userpassword '] != $password){
echo "密码输入错误,请重新登录!<br>";
}else{
//如果用户名密码都正确,注册一个session来标记其登录状态
session_start();
// $_SESSION["login"] = "YES";
echo "<script language=javascript>alert('登录成功!');location.href='send.php';</script>";
}
}
mysqli_close($link);
}
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>明哥-留言板登录页</title>
</head>
<body>
<table border=1 cellspacing=0 cellspadding=0 align=center width=400 height="358">
<caption>明哥在线留言系统</caption>
<tr>
<td align="center">
<a href=send.php>[我要写留言]</a>
<a href=login.php>[管理留言]</a>
</td>
</tr>
<tr>
<td >
<form method="post" action="login.php">
<table border="1" width="100%" id="table1" cellspcing="0" cellpadding="0" >
<tr>
<td colspan="2" >
<p align="center">欢迎管理员登录</p>
</td>
</tr>
<tr>
<td >
<p align="center">用户名</P>
</td>
<td >
<input type="text" name="name" size="20">
</td>
</tr>
<tr>
<td>
<p align="center">密 码</p>
</td>
<td>
<input type="password" name="password" size="20">
</td>
</tr>
<tr>
<td colspan="2">
<p align="center"><input type="submit" value="登录" name="B1"></p>
</td>
</tr>
</table>
</form>
</td>
</tr>
<tr>
<td align=center>
<a href="https://blog.csdn.net/weixin_40845165">版权归明哥所有</a><br>
</font>
</td>
</tr>
</body>
</html>
留言发布页:send.php
<?php
error_reporting(E_ALL&~E_NOTICE&~E_WARNING);
header("Content-Type: text/html;charset=utf-8");
date_default_timezone_set('PRC');
$name = $_POST["name"];//从input里面传过来的name
//看用户是否提交了新留言,如果提交了,则写入表message
if( $name != ""){
$content = $_POST["content"];
//下面的代码用于获得当前日期和时间
$addtime = date("Y-m-d h:i:s");//得到日期
$link = mysqli_connect("127.0.0.1","root","root");//PHP连接数据库
if( $link)
echo "ok!<br>";
else {
echo "bad!<br>";
}
mysqli_select_db($link,"board");//选择数据库
$insert = "insert into message(author,addtime,content) values('$name','$addtime','$content')";
mysqli_query($link,$insert);
mysqli_close($link);
echo "<script language=javascript>alert('留言成功!单击确定查看留言.');location.href='index.php';</script>";
}
mysqli_close($link);
?>
<!doctype html>
<html>
<head>
<title>明哥在线留言系统</title>
</head>
<body>
<table border=1 cellspacing=0 cellspadding=0 align=center width=700 >
<caption>明哥在线留言系统</caption>
<tr>
<td align="center">
<a href=login.php>[用户登录]</a>
<a href=index.php>[留言列表]</a>
</td>
</tr>
<tr>
<td height=200>
<form method="POST" action="send.php">
<table border="1" width="100%" id="table1" cellspacing="0" cellpadding="0" height="400">
<tr >
<td align="center">
<p>用户名</p>
</td>
<td >
<input type="text" name="name" placeholder="请输入您的用户名">
</td>
</tr>
<tr >
<td align="center" >
<p>留言内容</p>
</td>
<td>
<textarea rows="20" name="content" cols="50" placeholder="请输入留言内容"></textarea>
</td>
</tr>
<tr>
<td colspan="2">
<p align="center">
<input type="submit" value="提交" name="B1">
</p>
</td>
</tr>
</table>
</form>
</td>
</tr>
<tr>
<td height=80 align=center>
<a href="https://blog.csdn.net/weixin_40845165">版权归明哥所有</a><br>
</font>
</td>
</tr>
</table>
</body>
</html>
留言展示页:index.php
<!-- 2.留言本首页 index.php -->
<!-- 本页面显示十条最近的的留言,并且有分页功能 -->
<html>
<head>
<title>明哥在线留言系统</title>
<style type="text/css">
TD{
font-size: 12px;
line-height: 150%;
}
</style>
</head>
<body>
<table border=1 cellspacing=0 cellspadding=0 align=center width=600 height=500>
<caption>明哥在线留言系统</caption>
<tr>
<td align="center">
<a href=send.php>[我要写留言]</a>
<a href=login.php>[管理留言]</a>
</td>
</tr>
<tr>
<td height=200>
<?php
error_reporting(E_ALL&~E_NOTICE);
header("Content-Type: text/html;charset=utf-8");
date_default_timezone_set('PRC');
$link = mysqli_connect("127.0.0.1","root","root");
mysqli_select_db($link,"board");
$query = "select * from message";
$result = mysqli_query($link,$query);
if( mysqli_num_rows($result) < 1){
echo " 目前数据表中还没有任何留言!";
}else{
$totalnum = mysqli_num_rows($result);//获取数据库中所有数据条数
$pagesize = 7;//每页显示7条
$page = $_GET["page"];
if( $page == ""){
$page = 1;
}
$begin = ($page-1)*$pagesize;
$totalpage = ceil($totalnum/$pagesize);
//输出分页信息
echo "<table border=0 width=95%><tr><td>";
$datanum = mysqli_num_rows($result);
echo "共有".$totalnum."条留言,每页".$pagesize."条,共".$totalpage."页。<br>";
//输出页码
for( $i = 1; $i <= $totalpage; $i++){
echo "<a href=index.php?page=".$i.">[".$i."] </a>";
}
echo "<br>";
//从message表中查询当前页面所要显示的留言,并根据时间排序
$query = "select * from message order by addtime desc limit $begin,$pagesize";
$result = mysqli_query($link,$query);
$datanum = mysqli_num_rows($result);
//循环输出所有留言,如果管理员已经回复则同时输出回复
for( $i = 1; $i <= $datanum; $i++){//$datanum???
$info = mysqli_fetch_array($result);
echo "->[".$info['author']."]于".$info['addtime']."说:<br>";
echo " ".$info['content']."<br>";
if( $info['reply'] != ""){
// <b></b>显示粗体
echo "<b>管理员回复:</b>".$info['reply']."<br>";
}
echo "<hr>";
}//else结束
echo "</td></tr></table>";
}
mysqli_close($link)
?>
</td>
</tr>
<tr>
<td height=80 align=center>
<a href="https://blog.csdn.net/weixin_40845165">版权归明哥所有</a><br>
</font>
</td>
</tr>
</table>
</body>
</html>