小白简单留言板-总代码

大概如图:

HTML:

1.<form action=”php文件地址”  method=”post”>   以post方法将表格提交的数据传向该php文件,处理表格文件必须要外加form标签。

2.textarea中的autofocus 进入后自动把焦点放在该表格, placeholder:提示内容,输入内容后将消失。

3.表格中的name在php处理数据时会使用,必须要加!

创建数据库:

1.本小白通过navicat创建数据库,创建数据库mydb,创建msg表用来储存信息,有id,user,content,time,其中id为主键自动递增,设置好字符集,排序规则。

数据库数据传入网页

1.把HTML格式改为php格式

2. $db = new mysqli($host, $user, $pwd, $dbname);当$db->connect_errno !=0时即为连接失败,用die(“连接失败”)提示结束,找原因。

3.设置数据库字符集:$db->query(“SET NAMES UTF8”);//可将整个连接过程放入该目录的另一个文件如connect.php,要是用时可用include(‘connect.php’)调用,这样很方便,因为网页数据传入数据库也需要连接数据库;

4.$sql = “select * from msg order by id desc”;//sql语句存储,将数据库中的信息以id反向排序;

5. $db->query($sql);// 用来在数据库中执行此sql语句(上面),返回true或者false,可用来判断获取信息是否成功

6. $mysqli_result->fetch_array(MYSQL_ASSOC)// 获取$mysqli_result中最先的一行数据

7.在遍历$mysqli_result时 $rows[ ] = $row;  //把从数据库获得的数据存入$rows[ ],找了半天的bug。。。。

8.在网页输出板块中添加PHP代码,在foreach循环中把储存的$rows[ ]分别存入$row并输出到网页。

9. date("Y-m-d H:i:s", $row['time'] //使用date(“格式”,$row[‘time’]),格式化日期;

网页数据传入数据库

  1. $user = $_POST[‘user’];$content = $_POST[‘content’];//【】里的就是表格中的name属性
  2. 创建自己写的Input对象(构造函数为 public function __construct($user ,$content )),使用自己写的judge方法判断数据输入是否正确,能否插入数据库,在函数中应用都要用$this->   如$this->user == ‘’;//判断是否为空;
  3. $time = time();//time() 函数返回自 Unix 纪元(January 1 1970 00:00:00 GMT)起的当前时间的秒数,
  4. $sql = "insert msg(user,content,time) values('{$user}' ,'{$content}', '{$time}')";//将sql语句存起来。
  5. $db->query(“sql语句”);//用来在数据库中执行此sql语句(上面),返回true或者false,可用来判断信息是否入数据库成功
  6. header(“location:gbook.php”); //立刻返回原网页并刷新

 网页代码:

<?php
//将数据库的信息传入网页;
include("connect.php");//连接数据库 $sql = "select * from msg order by id desc"; $mysqli_result = $db->query($sql); if($mysqli_result === false){ die("sql语句错误"); } //else echo "获取数据库信息成功"; $rows = []; //$mysqli_result->fetch_array(MYSQL_ASSOC))获取$mysqli_result中最先的一行数据 while($row = $mysqli_result->fetch_array(MYSQL_ASSOC)){ $rows[] = $row;//把每一行数据存入rows[]中 //echo "用户名:{$row['user']} <br/>"; //echo "内容;{$row['content']} <br/>"; } //var_dump($rows);//判断是否获取成功 ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>留言板</title> <style type="text/css"> .head {text-align:center;color:#FAA261;} .wrap { width:600px; margin:0px auto; //background:#390; } .input .content{ margin-bottom:5px; width:594px; height:150px; } .input .submit{float:right;} .output {margin-top:5px;background:#CCC;padding:3px;margin-top:20px;} .output .user{color:#95F;} .output .time{float:right;color:#6C0;} </style> </head> <body> <div class="wrap"> <h1 class="head">留言板</h1> <!--输入板块 --> <div class="input"> <form action="save.php" method="post"> <textarea name="content" class="content" autofocus="autofocus" placeholder="请输入留言内容:" ></textarea><br/> <label for="text">用户名:</label> <input name="user" type="text" class="user"/> <input type="submit" class="submit" value="提交留言"/> </form> </div> <!--输出板块--> <?php foreach($rows as $row){ ?> <div class="output"> <span class="user">用户:<?php echo $row['user']; ?></span> <span class="time"><?php echo date("Y-m-d H:i:s",$row['time']);?></span> <div class="content"> <?php echo $row["content"]; ?> </div> </div> <?php } ?> </div> </body> </html>

连接数据库connect.php代码:

<?php
    $host = "127.0.0.1";
    $user = "root";
    $pwd = "root";
    $dbname = "mydb";
    $db = new mysqli($host, $user, $pwd, $dbname);
    //var_dump($db);
    if($db->connect_errno != 0){//判断数据库是否连接成功
        echo $db->connect_errno;
        die("数据库连接失败");
    }
    //else echo"成功";
    $db->query("SET NAMES UTF8");//设置数据库传输数据的编码,不然乱码
?>

用来判断数据是否合法的Input.php代码:

<?php
    class Input{
        public $user;public $content;
        public function __construct($user, $content){//构造函数
            $this->user = $user;
            $this->content = $content;
            echo "<br/>数据初始化成功,即将写入数据。。。。。。。。<br/>";    
        }
        public function judge(){
            $forbid = ['你妈妈', '你爸爸', '你个傻逼东西'];//禁止的用户名
            foreach($forbid as $user0){//遍历forbid,把$forbid中每个数据分别放入user0
                if($user0 == $this->user or $this->user==''){//进行判断数据是否合法
                    die("用户名错误");//die能直接结束并输出,不执行下面的语句
                }
            }
            if($this->content == '')
                die("内容不能为空");
            echo "成功获取<br/>用户名:{$this->user}<br/>内容:{$this->content}<br/>";
        }
    }
?>

用来接收表单信息的save.php代码

<?php
    include('connect.php');
    include("Input.php");//导入对象Input,用来判断输入的数据是否合理
    $user = $_POST['user'];
    $content = $_POST['content'];
    echo "接收数据成功:<br/>用户: $user <br />内容: $content  ";
    $input = new Input($user, $content);
    $input->judge();
    $time = time();
    $sql = "insert msg(user,content,time) values('{$user}' ,'{$content}', '{$time}')";
    //echo $sql;
    $judge = $db->query($sql);
    if($judge === false){echo "sql错误";}
    else echo "已经成功插入";
    header("location:gbook.php");//返回原网页并刷新,注析后可看执行执行过程
?>

猜你喜欢

转载自www.cnblogs.com/first-bloodlalala/p/11697945.html