初学PHP实现简单的留言板 -> php+MySQL+Apache

效果图:

1.在文本框输入内容后,点击发表留言,效果如下如所示.

 2.数据库中则将数据记录了下来.


实现代码:

gbook.php -> 数据可视化界面

<?php
/**
 * 处理字符集编码
 */
header("Content-Type: text/html;charset=utf-8");

/**
 * 数据库连接参数
 */
$host = '127.0.0.1';
$user = 'root';
$pwd = 'root';
$dbname = 'php10';
$db = new mysqli($host, $user, $pwd, $dbname);

/**
 * 检查连接是否成功
 */
if ($db->connect_errno <> 0) {
    echo '连接失败';
    echo $db->connect_error;
    exit;
}

/**
 * 设置数据库字符集编码
 */
$db->query("SET NAMES UTF8");

/**
 * 数据库查询
 */
$sql = "select * from msg order by id desc";
$mysqli_result = $db->query($sql);
// 如果返回的不是对象,是false则提示错误信息.
if ($mysqli_result === false) {
    echo "SQL错误";
    exit;
}

/**
 * 首次调用显示最新的一条记录
 * 重复调用,依次显示后面的记录
 * 如果没有记录可以显示,就返回null 
 */
/**
 * row = array(01,2) == true
 * row = null == false
 */
$rows = [];
while ($row = $mysqli_result->fetch_array(MYSQL_ASSOC)) {
    $rows[] = $row;
}

?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>留言本</title>
    <style>
        .wrap {
            width:600px;
            margin:0px auto;
        }
        .add{
            overflow:hidden;
        }
        .add .content {
            width:598px;
            margin:0;
            padding:0;
        }
        .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="save.php" method="post">
                <textarea name="content" class="content" cols="50" rows="5"></textarea>
                <p>
                    &nbsp;&nbsp;留言人<input type="text" name="user" class="user" />
                    <input type="submit" class="btn" value="发表留言" />
                </p>
                
            </form>
        </div>

        <?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['intime']); ?></span> -->
            </div>
            <div class="content">
                <?php echo $row['content']; ?>
            </div>
        </div>
        
        <?php
    }
    ?>
    </div>
</body>
</html>

input.php -> 输入检查类

<?php
class input {
    // 定义函数,对数据进行检查
    function post ($content) {
        if ($content == '') {
            return false;
        }

        // 禁止使用的用户名
        $n = ['张三', '李四', '王五'];

        foreach ($n as $name) {
            if($content == $name) {
                return false;
            }
        }
        return true;
    }
}
?>
扫描二维码关注公众号,回复: 3654327 查看本文章

connect.php -> 数据库连接类

<?php

/**
 * 连接数据库的参数
 */
$host = '127.0.0.1';
$dbuser = 'root';
$pwd = 'root';
$dbname = 'php10';

/**
 * 连接到数据库
 * 实例化mysqli,将前面的四个参数添加到实例化对象中
 */
$db = new mysqli($host, $dbuser, $pwd, $dbname);
// 如果连接失败则执行
if ($db -> connect_error <> 0) {
    die('连接数据库失败');
}

/**
 * 设定数据库数据传输的编码
 */
$db -> query("SET NAMES UTF8");

?>

 save.php

<?php

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

// 处理字符集编码
header("Content-Type: text/html;charset=utf-8");

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

$input = new input();

// 调用函数,检查留言内容
$is = $input -> post($content);
if ($is == false) {
    die('留言板内容的数据不能为空');
}

// 调用函数,检查留言人
$is = $input -> post($user);
if($is == false) {
    die('留言人的数据不正确');
} 

$time = time();
$sql = "insert into msg (content, user, intime) values ('{$content}','{$user}','{$time}')";
$is = $db -> query($sql);
// var_dump($is);
// 跳转网页
header("location: gbook.php");

?>

GitHub下载地址: https://github.com/godlikecheng/Messages

猜你喜欢

转载自blog.csdn.net/qq_40820862/article/details/83010231