发布留言界面
查看留言界面
建立数据库创建表
//conn.php <?php $conn = MySQLi_connect("localhost", "root", "root") or die("数据库链接错误"); mysqli_select_db($conn,"bbs"); mysqli_query($conn, "set names utf8"); ?>
数据库
创建发布留言界面
//add.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>留言页面</title>
<link rel="stylesheet" type="text/css" href="css.css">
<?php include ("add.php")?> //发布留言数据插入数据库
</head>
<body>
<script>
function CheckPost() {
if(myform.user.value=="")
{
alert("请填写用户");
myform.user.focus();
return false;
}
if (myform.title.value.length<5)
{
alert("标题不能少于5个字符");
myform.title.focus();
return false;
}
if (myform.content.value=="")
{
alert("内容不能为空");
myform.content.focus();
return false;
}
}
</script>
<b> <a href="list.php">浏览留言</a> </b>
<hr size=1>
<form action="add.php" method="post" name="myform" onsubmit="return CheckPost();">
用户:<input type="text" size="10" name="user"/><br>
标题:<input type="text" name="title" /><br>
内容:<textarea name="content"></textarea><br>
<input type="submit" name="submit" value="发布留言" />
</form>
</body>
</html>
创建发布留言add.php页面
//add.php
<?php
include ("conn.php");
$id=$_POST['id'];
$user=$_POST['user'];
$title=$_POST['title'];
$content=$_POST['content'];
if($_POST['submit']){
$sql="insert into message(id,user,title,content,lastdate)values('','$user','$title','$content',now())";
mysqli_query($conn,$sql);
echo "<script>alert('提交成功!返回首页。');location.href='add.html';</script>";
}
?>
创建查看留言页面
//list.php
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>浏览留言</title>
<link rel="stylesheet" type="text/css" href="views/css.css">
<?php include ("conn.php"); ?>
</head>
<body>
<table width=500 border="0" align="center" cellpadding="5" cellspacing="1" bgcolor="#add3ef" >
<?php
$sql="select * from message order by id DESC"; //查询数据库
$query=mysqli_query($conn,$sql);
while($row=mysqli_fetch_array($query)){ ?>
<tr bgcolor="#eff3ff">
<td><font color="red">标题:</font>
<?php echo $row['title'];?>
<font color="red">用户:</font>
<?php echo $row['user'];?>
<div align="right">
<a href="del.php?id=<?php echo $row['id'];?>">删除</a>
</div>
</td>
</tr>
<tr bgColor="#ffffff">
<td>发表内容:<?php echo $row['content'];?></td>
</tr>
<tr bgColor="#ffffff">
<td><div align="right">时间:<?php echo $row['lastdate'];?></td>
</tr>
<?php } ?>
<tr bgcolor="#f0fff0">
<td><div align="right"><a href="add.html">返回留言</a> </td>
</tr>
</table>
</body>
</html>
创建删除留言del.php页面
<?php include 'conn.php'; $id = $_GET['id']; $query="delete from message where id=".$id; mysqli_query($conn,$query); //页面跳转,实现方式为JAVAscript $url = "list.php"; echo "<script>"; echo "window.location.href='$url'"; echo "</script>"; ?>