首页 > 建站教程 > PHP教程 >  php实现简单的留言板正文

php实现简单的留言板

之前,我爱模板网分享过用asp做一个简单的留言板,可以点击下面链接查看:
手把手教你用ASP制作留言本1
手把手教你用ASP制作留言本2
手把手教你用ASP制作留言本3
手把手教你用ASP制作留言本4

这里分享一个用php做的简单留言板界面

1、首先是留言界面,包括添加新留言、对留言进行编辑和删除、登录和退出登陆。先看下效果图



下面是php文件代码:
<?php session_start(); ?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<?php
if (isset($_SESSION["uid"])){
    echo "欢迎你:" . $_SESSION["username"];//开头设置session这里调用session
    echo "<br>";
    $uid = $_SESSION['uid'];//活动当前用户的uid
    echo "<a href='pcenter.php?uid=$uid'>个人中心</a>";
    echo "<br>";
    echo "<a href='123.php'>退出</a>";
}else{
    die("无权访问,请登录后查看!<meta http-equiv='Refresh' content='0;url=123.php'/>");
}
?>
<center>
    <title>留言</title>
</head>
<body>
<h1>欢迎你!</h1>
<table border="0" cellpadding="30">
<tr>
<th>留言人</th>
<th>留言</th>
<th>ip</th>
<th>性别</th>
<th>时间</th>
</tr>
<?php
include "conn.php";
$sql = "select * from message";
$result = mysqli_query($conn,$sql);
while ($message = mysqli_fetch_array($result) ) {
$uid = $message['uid'];
echo "<tr>";
    $sql1 = "select username,sex from user where uid =$uid";//通过uid获得当前用户的名字和性别
    $result1 = mysqli_query($conn,$sql1);
    $persion = mysqli_fetch_array($result1);
    $user = $persion["username"];
    $sex = $persion["sex"];
    echo "<td>". $user ."</td>";
    echo "<td>". $message["content"] ."</td>";
    echo "<td>". $message["ip"] ."</td>";
    echo "<td>". $sex ."</td>";
    echo "<td>". $message["time"] ."</td>";
    //echo $message["uid"];
    echo "<br>";
    //echo $_SESSION["uid"];
    if ($message["uid"] == $_SESSION["uid"]) //判断登陆人的uid和留言人的uid时候一样,如果一样就是一个人,可以对留言进行操作
    {
        
     echo "<td> <a href='bianji.php?mid=".$message["id"]."'>编辑</a>  <a href='del.php?mid=".$message["id"]."'>删除 </a></td>";//编辑和删除是单独对每条留言进行操作,每条留言都有一个id
    //echo "</tr>";
}else{
    echo "<td>编辑  删除</td>";
}
echo "</tr>";
}
?>
</table>
<form method="post" action="message.php">
<textarea name="content" rows="10" cols="80"></textarea>
<br />
<input type="submit" name="submit" value="添加留言"> 
<input type="reset" name="reset" value="重置">
</center>
</body>
</html>
2、对留言的编辑功能包括只能对当前用户留的言进行编辑,然后保存。先看下效果图:



代码:
<meta charset="utf-8">
<?php
include "conn.php";
//$mid = $_GET["mid"];
//$result = mysqli_query($conn,$sql);
//$message = mysqli_fetch_array($result);
if (isset($_POST["submit"])) {
$cmid = $_POST["cmid"];
$content = $_POST["content"];
if(!empty($content)){
$sql2 = "update message set content='$content' where id=$cmid";
//echo $sql2;
$result1 = mysqli_query($conn,$sql2);
if ($result1) {
            echo "<script>alert('编辑成功')</script>";
        echo "<script>window.location.href='ly.php'</script>";
 
}else{
      die("数据库错误" .mysqli_error($conn));
  }
}else{
    echo "留言不能为空!";
}
}else{
    
    $mid = $_GET["mid"];
    $sql = "select * from message where id = $mid";
$result = mysqli_query($conn,$sql);
$message = mysqli_fetch_array($result);
}
 
?>
<!DOCTYPE html>
<html>
<head>
    <title>留言编辑</title>
</head>
<body>
<form method="post" action="bianji.php">
<textarea name="content" rows="10" cols="80" ><?php echo $message["content"];?></textarea><!--将原来的留言放到编辑框中进行编辑-->
<br />
<input type="hidden" name="cmid" value="<?php echo $_GET["mid"];?>"><!--设置一个隐藏属性,获得留言的id-->
<input type="submit" name="submit" value="添加留言"> 
<input type="reset" name="reset" value="重置">
</form>
</body>
</html>
3、删除留言,只能删除当前用户的留言



代码:
<meta charset="utf-8">
<?php
include "conn.php";
$mid = $_GET["mid"];//获得该条留言的id
$sql = "delete from message where id=$mid";//通过id删除这条留言
$result = mysqli_query($conn,$sql);
if($result){
    echo "<script>alert('留言删除成功!')</script>";
    echo "<script>window.location.href='ly.php'</script>";
    
}