以留言板入门PHP

1、任务:写一个留言板

能够实现留言并显示。

2、HTML+CSS实现留言板大致模样

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>留言板</title>
    <style>
        .container{
            width: 600px;
            margin: 0px auto;

        }
        .inputs{
            overflow: hidden;
        }
        .inputs .content{
            width: 598px;
            margin: 0px;
            padding: 0px;
        }
        .inputs .user{
            float: left;
        }
        .inputs .btn{
            float: right;
        }
        .msg{
            width: 598px;
            background-color: antiquewhite;
            margin: 10px auto;
        }
        .msg .time{
            float: right;
        }
    </style>
</head>
<body>
    <div class="container">
        <!--留言区域-->
        <form action="save.php" method="post">
            <div class="inputs">
                <textarea  name="content" class="content" cols="30" rows="10"></textarea>
                <br/>
                <input name="user" class="user" type="text">
                <input class="btn" type="submit" value="发表留言">
            </div>
        </form>

        <!--查看留言-->
        <div class="msg">
            <div class="info">
                <span class="user">用户名</span>
                <span class="time">2011-1-1 11:11:00</span>
            </div>
            <div class="content">
                此处是留言内容
            </div>
        </div>
        <div class="msg">
            <div class="info">
                <span class="user">用户名</span>
                <span class="time">2011-1-1 11:11:00</span>
            </div>
            <div class="content">
                此处是留言内容
            </div>
        </div>
        <div class="msg">
            <div class="info">
                <span class="user">用户名</span>
                <span class="time">2011-1-1 11:11:00</span>
            </div>
            <div class="content">
                此处是留言内容
            </div>
        </div>
    </div>
</body>
</html>

3、数据库

在数据库创建相应的表和字段

此处的id值要选择自动递增,否则留言时再插入数据会因为id值重复,而插入失败,最后就只会有一条数据。

连接数据库并new一个PDO对象

<?php
    $host = '127.0.0.1';
    $user = 'root';
    $pwd = '';
    $dbname = 'message';

    $db=new PDO("mysql:host=$host;dbname=$dbname;",$user,$pwd);
//此处dsn部分不能用单引号,因为里面用了变量,单引号是无法识别的,
//单引号与双引号的一个区别就是双引号可以识别变量,所以处理速度相较于单引号慢

//    $db = new mysqli( $host, $user, $pwd, $dbname );

    $db->query("SET NAMES UTF8");

4、留言板输入内容存入数据库

<?php

    include ('connect.php');
    include('input.php');

    $content=$_POST['content'];
    $user=$_POST['user'];

    $input = new input();

    $return = $input->post($content);
    if($return == false){
        die('留言内容拒绝');
    }

    $return = $input->post($user);
    if ($return == false){
        die('留言人拒绝');
    }

    $time = time();
    $sql = "INSERT INTO msg (content, user, time) values('{$content}', '{$user}', '{$time}')";
    $db->query($sql);
    header("location: index.php");

5、对输入内容进行过滤

<?php
    class input{

        function post( $content){
            if($content ==''){
                return false;
            }

            $forbid_array = ['张三' , '李四'];

            foreach ( $forbid_array as $forbid_name){
                if( $content == $forbid_name){
                    return false;
                }
            }
            return true;
        }
    }

6、输出数据库内容

<?php
    include ('connect.php');

    $sql_out="SELECT * FROM msg ORDER BY id DESC ";

    $result = $db->query($sql_out);

//    $result->setFetchMode(PDO::FETCH_ASSOC);
//不设置的话,fetch后会返回两种数组,关联数组和数字索引数组,FETCH_ASSOC就是选取关联数组
//两种方法,上下面的都行,主要于mysqli区别是,要加PDO::

//    while ($row = $result->fetch(PDO::FETCH_ASSOC) ){
//        //fetch后会返回两种数组,关联数组和数字索引数组,FETCH_ASSOC就是选取关联数组
//        $rows[]=$row;
//    }
//上下两种方法都行,fetch是取一组,fetchall是全部
    $rows = $result->fetchAll(PDO::FETCH_ASSOC);

7、将原来的html调整至到index.php

<?php
    include('output.php');
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>留言板</title>
    <style>
        .container{
            width: 600px;
            margin: 0px auto;

        }
        .inputs{
            overflow: hidden;
        }
        .inputs .content{
            width: 598px;
            margin: 0px;
            padding: 0px;
        }
        .inputs .user{
            float: left;
        }
        .inputs .btn{
            float: right;
        }
        .msg{
            width: 598px;
            background-color: antiquewhite;
            margin: 10px auto;
        }
        .msg .time{
            float: right;
        }
    </style>
</head>
<body>
<div class="container">
    <!--留言区域-->
    <form action="save.php" method="post">
        <div class="inputs">
            <textarea  name="content" class="content" cols="30" rows="10"></textarea>
            <br/>
            <input name="user" class="user" type="text">
            <input class="btn" type="submit" value="发表留言">
        </div>
    </form>
    <?php
    foreach ($rows as $row){
    ?>
        <!--查看留言-->
        <div class="msg">
            <div class="info">
                <span class="user"><?php echo $row['user'] ?></span>
                <span class="time"><?php echo date("Y-m-d H-i-s", $row['time']) ?></span>
            </div>
            <div class="content">
                <?php echo $row['content'] ?>
            </div>
        </div>
    <?php
    }
    ?>
</div>
</body>
</html>

视频学习:https://study.163.com/course/introduction/1004572025.htm

猜你喜欢

转载自www.cnblogs.com/As-usua1/p/12311368.html