PHP留言板

1.展示。

在这里插入图片描述

2.数据库结构。

  数据库名称为liuyan,表名为liuyan,表中结构如下所示:
在这里插入图片描述
  可以使用以下sql语句,在命令行中快速创建数据库和数据表:

CREATE DATABASE liuyan;
USE liuyan;
CREATE TABLE IF NOT EXISTS `liuyan` (
  `id` int(5) NOT NULL AUTO_INCREMENT,
  `username` varchar(50) NOT NULL,
  `text` tinytext NOT NULL,
  `time` datetime NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
3.代码。
2.1 conn.php
<?php
    // 连接数据库
    $servername="localhost";
    $username="root";
    $password="root";
    $dbname="liuyan";
    // 创建链接
    $conn = new mysqli($servername,$username,$password,$dbname);
    // 检测链接
    if($conn->connect_error){
        die("连接失败:".$conn->connect_error);
    }
?>

  这是数据库连接代码,如果数据库连接失败会打印出失败原因。

2.2 config.php
<?php
//链接数据库
require_once 'conn.php';
//全站标题
$title = "我的留言板";

  这是配置文件,包含了一次数据库连接文件,设定的全站title。

2.3 index.php
<?php
require_once 'config.php';
?>

<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?php echo $title; ?></title>
</head>

<body>
    <h1><a href="./index.php"><?php echo $title; ?></a></h1>

    <h2>写留言</h2>
    <form action="add.php" method="GET">
        <textarea name="t" cols="30" rows="10" placeholder="说点什么..."></textarea>
        <p><input name="n" type="text" placeholder="你的名字"></p>
        <p><input type="submit" value="发表"></p>
    </form>
    <hr>
    <h2>留言列表</h2>
    <ul>
        <?php
        // 最新留言展示前面
        $sql = "SELECT * FROM `liuyan` ORDER BY `liuyan`.`id` DESC";
        // ORDER BY `liuyan`.`id` DESC 加上这个是降序排列
        $result = $conn->query($sql);
 
        if($result->num_rows>0  ){
            //输出数据
            while($row = $result->fetch_assoc()){
                // $result->fetch_assoc()执行一次显示第一条,执行第二次显示第二条
            ?>
            <li>
                <p><?php echo $row["id"];?>楼</p>
                <p>留言内容:<?php echo $row["text"];?></p>
                <p>留言人:<?php echo $row["username"];?></p>
                <p>留言时间:<?php echo $row["time"];?></p>
    
                <p>
                    <a href="edit.php?id=<?php echo $row['id'];?>">编辑</a>
                    <a href="del.php?id=<?php echo $row['id'];?>">删除</a>
                </p>
            </li>
            <?php
            }
        } else {
            echo"暂无留言!";
        }
        ?>        
    </ul>
</body>
</html>

  这是首页文件,包含了一次配置文件,主要实现了留言板的前端显示。在html代码中插入了PHP,实现了数据库查询,使用了 ORDER BY liuyan.id DESC 实现了对查询数据的降序排列,并将查询结果赋值到result变量。在通过if($result->num_rows>0)判断查询结果的行数是否大于0,如果大于0,执行while循环,打印出查询结果。否则,打印“暂无留言!”

2.4 add.php
<?php
require_once 'config.php';

$t = $_GET["t"];
$n = $_GET["n"];
$time = date("Y-m-d H:i:s",time());

//插入语句
$sql = "INSERT INTO `liuyan` (`id`, `username`, `text`, `time`) VALUES (NULL, '$n', '$t', '$time');";
$conn->query($sql);
header("Location:index.php");
?>

  这是添加留言文件,包含一次配置文件,通过$_get方法搜集URL:http://127.0.0.1/bbs/add.php?t=1&n=1中,t和n的值,及用户名和留言内容,再将其赋值到插入语句中。

2.5 edit.php
 <?php
//加载配置文件
require_once "config.php";
//GET方法接受传过来的id
$id = $_GET['id'];
//根据id查到当前的具体信息
$sql = "SELECT * FROM `liuyan` WHERE `id` = $id";
//执行sql的查询语句,结果赋值给result
$result = $conn->query($sql);
//得到当前id的留言内容
if ($result->num_rows > 0) {
    $res = $result->fetch_assoc();
    $text = $res["text"];
}
 else {
    die("无此条留言");
}
?>

<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>留言信息编辑</title>
</head>

<body>
    <h1><?php echo $title; ?></h1>
    <h2>编辑<?php echo $id;?>楼的留言内容</h2>
    <form action="updata.php" method="GET">
        <input hidden name="i" value="<?php echo $res['id'] ?>" type="text">
        <textarea name="t" cols="30" rows="10"><?php echo $res["text"] ?></textarea>
        <input type="submit" value="更新留言信息">
    </form>
</body>

</html>

  这是修改页面,实现了修改页面的前端显示。逻辑是通过$_GET方法,获取当前的id值,再通过查询语句将查询结果打印输出。

2.6 updata.php
<?php
require_once "config.php";

//点击之后传了个id过来
//这边php文件通过GET接受传过来的id和2个值
$id = $_GET['i'];
$t = $_GET['t'];

var_dump($id);
var_dump($t);


//sql的更新代码
$sql = "UPDATE `liuyan` SET `text` = '$t' WHERE `liuyan`.`id` = $id;";
//执行sql更新语句
$conn->query($sql);

//回到首页
header("Location:index.php");
?>

  这是编辑更新数据文件,包含一次配置文件,通过$_GET获取id值和text值,通过执行更新语句,对数据表中内容进行更新,最后通过header(“Location:index.php”);返回首页。

2.7 del.php
<?php

//加载配置文件
require_once "config.php";

//点击之后传了个id过来
//这边php文件通过GET接受传过来的id
$id = $_GET['id'];

//删除语句
$sql = "DELETE FROM `liuyan` WHERE `liuyan`.`id` = $id";
//执行sql语句
$conn->query($sql);
//$conn->query($sql1);

//返回到index.php
header("Location:index.php");

  这是删除留言文件,通过获取id值,通过id值,执行条件删除语句。删除完成后跳转回首页。

  • 4
    点赞
  • 73
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

晶晶娃在战斗

你的鼓励将是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值