简单留言板(笔记)

来源:互联网 发布:宁波plc编程招聘 编辑:程序博客网 时间:2024/05/22 03:47

需要创建4个PHP文件

index.php,首页,用户填数据

代码:

<!DOCTYPE html>
<html>
<head>
<title>我的留言板</title>
</head>
<body>
<center>
<a href="index.php">添加留言</a>
<a href="show.php">查看留言</a>
  <h3>添加留言</h3>
  <form action="doAdd.php" method="post">
    <table>
     <tr>
         <td>姓名</td><td><input type="text" name="name"></td>
     </tr>
        <tr>
            <td>留言</td><td><textarea name = "content" row = "5" cols = "30"></textarea></td>
        </tr>
        <tr>
            <td><input type="submit" value="提交"></td>
            <td><input type="reset" value="重置"></td>
        </tr>
    </table>
  </form>
</center>
</body>
</html>




doAdd.php,接受提交数据,添加留言

代码:

<!DOCTYPE html>
<html>
<head>
<title>添加留言</title>
</head>
<body>
<center>
<a href="index.php">添加留言</a>
<a href="show.php">查看留言</a>
   <h3>添加留言</h3> 


   <?php


   $name = $_POST['name'];
   $content = $_POST['content'];
   $time = date("Y/m/d");
   $ip = $_SERVER['REMOTE_ADDR'];

低版本数据库
  /* $conn = mysql_connection("localhost","root","") or die("连接数据库失败");
   mysql_select_db("liuyan");
   $sql = "insert into `liuyan`(`name`, `content`, `ip`, `time`) VALUES ('$name','$content','$ip','$time')";
   $result = mysql_query($sql);
    if($result == true){
     echo "留言成功";
    }*/

高版本数据库
     $mysqli = new mysqli("localhost", "root", "", "liuyan");
     $sql = "insert into `liuyan`  (name, content, ip, time) VALUES ('$name','$content','$ip','$time')";
     $result = mysqli_query($mysqli,$sql);
    if ($result==true){
     echo "留言成功";
    }
   ?>




</center>
</body>
</html>



delete.php,接受提交数据,根据id删除留言

代码:

<!DOCTYPE html>
<html>
<head>
<title>我的留言板</title>
</head>
<body>
<center>
 <a href='index.php'>增加留言</a>
 <a href='show.php'>查看留言</a>
 <h3>我的留言板</h3>
 <?php
  $id = $_POST['myid'];
  $mysqli = new mysqli("localhost", "root", "", "liuyan");
  $sql = 'delete from liuyan where id = '.$id;


  $result=mysqli_query($mysqli,$sql);
  if($result==true){
   echo "删除成功";
  }
 ?>
 </center>
</body>
</html>



show.php,显示留言。

代码:

<!DOCTYPE html>
<html>
<head>
<title>我的留言板</title>
</head>
<style type="text/css">
#myname{
float: left;
color: red;
}
#delete{
float: right;
}
#mytime{
 float: left;
 color: #cccccc;

}
#mycontent{
 float: left;

}


#a{  width: 500px;
height: 100px;
border: 1px solid;
}
#b{
width: 500px;
border: 1px solid;
float: left;
}
#c{
width: 500px;
float: left;
}
</style>
<body>
<center>
<a href="index.php">添加留言</a>
<a href="show.php">查看留言</a>
  <h3>我的留言</h3> 
<?php
    $mysqli = new mysqli("localhost", "root", "", "liuyan");
    $sql = 'select * from liuyan';
    $result = mysqli_query($mysqli,$sql);


    while($row = mysqli_fetch_array($result,MYSQLI_ASSOC)){
   echo "
    <form method='post' action='delete.php'>
   <div id='a'>


           <div id='b'>
         <div id='myname'>留言人:$row[name];</div>

         <div id='mytime'>时间:$row[time]</div>
         <input type='hidden' value=$row[id] name='myid'>
         <div id='delete'><input type='submit' value='删除留言'></div>
           </div>

            <div id='c'>
         <div id='mycontent'>$row[content]</div>
             </div>

         </div>
    </form>
         <br>
         ";
}
   ?>
</center>
</body>
</html>

原创粉丝点击