用PHP编写留言板

<?php 
include("test.php");
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-type=text/html;charset=utf-8"/>
<title>留言板</title>
 <link href="NewFile" rel="SHORTCUT ICON">
</head>
<body>
<a href="select.php">查看留言</a>
<form action="add.php" method="post">
 用户 <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>
<table class="imagetable">
<tr>
        <th>用户</th>
         <th>姓名</th>
        <th>内容</th>
        <th>时间</th>
        <th>操作</th>
    </tr>
<?php 
$sql="select * from msg order by id desc";
 $result= $conn->query($sql);
if ($result->num_rows > 0) {
    while($row=$result->fetch_assoc()) {
        echo 
        " <tr><td>".$row["user"]."</td><td>".$row["tile"]."</td><td>".$row["content"]."</td><td>".$row["lastdate"]."</td>
  <td><a href='del.php?id=".$row["id"]."'>删除</a></td></tr>";
    }
} else {
    echo "0 结果";
}
?>        
 </table>

</body>
</html>

主页面

<?php
//$sqlconne=  mysql_connect("localhost","root","root","bbs");
//mysql_query("set names 'utf8'");//使用utf-8编码
// 创建连接
$conn = new mysqli("localhost","root", "root", "bbs");
// 检测连接
if ($conn->connect_error) {
    die("连接失败: " . $conn->connect_error);
}


?>

服务器连接页面

<?php
include("test.php");
$user=$_POST['user'];
$title=$_POST['title'];
$content=$_POST['content'];
if ($_POST['submit']) {
  $sql="insert into msg
      values('','$user','$title','$content',now())";
  echo '<meta http-equiv="Content-type=text/html;charset=utf-8"/>';
  if ($conn->query($sql) === TRUE) {
        echo "<script>alert('发布成功');location.href='index.php';</script>";
  } else {
      echo "Error: " . $sql . "<br>" . $conn->error;
  }
}
?>

添加留言功能

<?php
include("test.php");
$user=$_GET['id'];
$sql="delete from msg where id=".$user;
    echo '<meta http-equiv="Content-type=text/html;charset=utf-8"/>';
    if ($conn->query($sql) === TRUE) {
        echo "<script>alert('删除成功');location.href='index.php';</script>";
    } else {
        echo "Error: " . $sql . "<br>" . $conn->error;
    }
?>

删除留言功能

posted @ 2019-03-16 16:55  紅人  阅读(295)  评论(0编辑  收藏  举报