1、留言板显示页面:note.php
<?php $host = "127.0.0.1"; $user = "root"; $pwd = "zst@123"; $dbname = "php10"; $db = new mysqli($host, $user, $pwd, $dbname); if($db->errno <> 0){ echo "数据库连接失败"; exit; } $sql = "select * from msg where id>15 order by id desc"; $mysqli_result = $db->query($sql); if($mysqli_result == false){ echo "查询语句执行失败"; exit; } $rows = []; while($row = $mysqli_result->fetch_array(MYSQLI_ASSOC)){ $rows[] = $row; } //var_dump($rows); ?> <html> <head> <meta charset="UTF-8"/> <title>留言本</title> <!--<link rel="stylesheet" type="text/css" href="test.css"> --> <style type="text/css"> .wrap{ width: 800px; margin: 0px auto; } .add{ overflow:hidden; <!--消除float--> } .add .content{ width: 798px; margin: 0px; padding: 0px; } .add .user{ float: left; } .add .btn{ float: right; } .msg{ margin: 20px 0px; background: #ccc; padding: 5px; } .msg .info{ overflow: hidden; } .msg .user{ float:left; color: blue; } .msg .time{ float: right; color:999; } .msg .content{ width: 100%; } </style> </head> <body> <div class="wrap"> <!--发表留言--> <div class="add"> <form action="messageboard_class.php" method="post"> <textarea name="content" class="content" cols="50" rows="5"></textarea> <br/> <input name="user" class="user" type="text"/> <input class="btn" type="submit" value="发表留言"/> </form> </div> <!--查看留言 --> <?php //设置日期时区,否则会少7小时 date_default_timezone_set("Asia/Shanghai"); foreach ($rows as $key => $record) { ?> <div class="msg"> <div class="info"> <span class="user"><?php echo $record["user"]; ?></span> <span class="time"><?php echo date('Y-m-d H:i:s', $record["intime"]); ?></span> </div> <div class="content"> <?php echo $record["content"]; ?> </div> </div> <?php } ?> </div> </body> </html>
2、提交留言后台处理:messageboard_class.php
<?php include("messageboard_class_import.php"); //过滤留言名称的类 include("connect.php"); //数据库连接工具类 //var_dump($_POST); $content = $_POST['content']; $user = $_POST['user']; //使用包含文件中的类,初始化input类对象 $input = new input(); if($input -> valid($content) == false){ die("输入的内容不能为空!"); //die方法:停止程序,在页面上显示内容 } if($input -> valid($user) == false){ die("输入的用户名为空或不合法!"); } //var_dump($_POST); /* 插入语句 */ //设置日期时区,否则会少7小时 date_default_timezone_set("Asia/Shanghai"); $time = time(); echo date("Y-m-d H:i:s", $time); $sql = "insert into msg(content, user, intime) values('{$content}', '{$user}', '{$time}')"; $connectClass = new conClass(); $db = $connectClass->getDB(); $is = $db->query($sql); if($is == true){ echo "执行成功"; }else{ echo "执行失败"; } //重定向浏览器访问 header("Location:note.php"); /* //查询语句 $sql = "select * from msg where id > 15 order by id desc"; $con = new conClass(); $db = $con -> getDB(); $mysqli_result = $db ->query($sql); if($mysqli_result == false){ echo "SQL 错误"; exit; } //定义一个数组 $rows = []; while($row = $mysqli_result->fetch_array(MYSQLI_ASSOC)){ $rows[] = $row; //rows 数组不写下标,意思是从0开始; } var_dump($rows); //可以看出是一个二维数组,键从0开始,值是每一行的数据(一维数组表示) */ ?>
过滤留言名称的类 messageboard_class_import.php
<?php class input{ public function valid($info){ if($info == ""){ return false; } //禁止使用某个用户名留言 $note = ['张三', '李四', '王乐']; foreach($note as $name => $value){ //echo $name; if($info == $value){ return false; } } return true; } } ?>
数据库连接工具类connect.php
<?php class conClass{ //写入数据库 public function getDB(){ $host = "127.0.0.1"; $dbuser = "root"; $passwd = "zst@123"; $dbname = "php10"; $db = new mysqli($host, $dbuser, $passwd, $dbname); if($db->errno <> 0){ return null; } return $db; } } ?>