php+mysql留言板

本文介绍了利用Navicat创建数据库和表,建立数据库连接文件connect.php,以及创建主界面index.php来展示留言。通过PHP实现添加(add.php),删除(delete.php),编辑(edit.php+update.php)和查询(index.php)功能,构建了一个基本的在线留言板系统。
摘要由CSDN通过智能技术生成

1.工具:navicat+phpstorm+phpstudy

2.思路:

3.建库: CREATE DATABASE 库名

4.建表:

CREATE TABLE message 
(
	id int AUTO_INCREMENT,
	username VARCHAR(50) NOT NULL,
	title VARCHAR(50) NOT NULL,
	content VARCHAR(200) NOT NULL,
	addtime VARCHAR(50) NOT NULL,
	PRIMARY KEY(id)
)
charset = utf8;

5.数据库连接文件:connect.php

<?php
    $host = 'localhost';
    $username = 'root';
    $password = '****';//数据库密码
    $dbname = 'message_board';
    $port = '3306';

    $link = mysqli_connect($host, $username, $password, $dbname, $port);

    if (!$link)
    {
        echo 'connection failed';
    }

    mysqli_set_charset($link, 'utf8');//设置编码
    mysqli_select_db($link, 'message_board');//选择数据库
    date_default_timezone_set('Asia/Shanghai');//时区

6.主界面:index.php

<h1 align="center">留言板</h1>

<?php
    include_once 'connect.php';
    include_once 'add.php';
?>

<table border="1px" width="400px" align="center" >
    <?php
    $sql = "SELECT * FROM message ORDER BY addtime DESC";//倒序
    $query = mysqli_query($link, $sql);
    while ($row = mysqli_fetch_assoc($query))
    {
        ?>
        <tr>
            <td><?=$row['id']?>楼</td>
        </tr>
        <tr>
            <td>用户名 :<?=$row['username']?></td>
        </tr>
        <tr>
            <td>留言标题:<?=$row['title']?></td>
        </tr>
        <tr>
            <td>留言内容:<?=$row['content']?></td>
        </tr>
        <tr>
            <td>留言时间:<?=$row['addtime']?></td>
        </tr>
        <tr>
            <td>
                <a href="./edit.php?id=<?=$row['id']?>">修改</a>                   
                <a href="./delete.php?id=<?=$row['id']?>">删除</a>
            </td>
        </tr>
    <?php
    }
    ?>
</table>

7.增:add.php

<?php
include_once 'connect.php';

if ($_POST['submit'])
{
    $sql = "insert into message (username, title, content, addtime)
            values ('$_POST[username]', '$_POST[title]','$_POST[content]',now())";
    $query = mysqli_query($link, $sql);
    if(!$query)
    {
        echo '留言失败<br/>';
        echo mysqli_error($link);
    }
}
?>

<form action="index.php" method="post" align="center">
    用户名 :<input type="text" name="username"/><br/>
    留言标题:<input type="text" name="title"/><br/>
    <table width="268px" border="0px" align="center">
        <tr><td>留言内容:</td></tr>
        <tr><td><textarea name="content" rows="5" cols="33"></textarea></td></tr>
    </table>
    <table width="268px" border="0px" align="center">
        <tr>
            <td>
                <input type="submit" value="发布留言" name="submit"/>
            </td>
        </tr>
    </table>
</form>

8.删:delete.php

<?php
    include_once 'connect.php';
    include_once 'index.php';

    $id = $_GET['id'];
    $sql = "DELETE FROM message WHERE id=$id";//写数据库语句时用双引号
    $query = mysqli_query($link, $sql);
    //echo $query;//查看是否传值成功
    if (!$query)
    {
        echo '删除失败';
        echo mysqli_error($link);
    }

9.改:edit.php+update.php

        edit.php

<?php
    include_once 'connect.php';

    $id = $_GET['id'];
    //echo $id.'<br/>';//查看id是否传值成功
    $sql = "SELECT * FROM message WHERE id=$id";//写数据库语句时用双引号
    //echo $sql.'<br/>';//查看查询是否成功
    $query = mysqli_query($link, $sql);
    $row = mysqli_fetch_assoc($query);
?>
<form action="./update.php" method="post" align="center">
    <input hidden type="text" name="<?=$row['id']?>"/>
    <table width="268px" border="0px" align="center">
        <tr><td>修改内容:</td></tr>
        <tr><td><textarea name="content" rows="5" cols="33"><?=$row['content']?></textarea></td></tr>
    </table>
    <table width="268px" border="0px" align="center">
        <tr>
            <td>
                <input type="submit" value="更改留言" name="submit"/>
            </td>
        </tr>
    </table>
</form>

        update.php

<?php
    include_once 'connect.php';
    include_once 'index.php';

    $content = $_GET['content'];
    $id = $_GET['id'];
    $sql = "UPDATE message SET content=$content WHERE id=$id";
    $query = mysqli_query($link, $sql);

10.查:index.php (查询结果展示在主界面中)

        就是这部分语句

 

11.效果展示:

        主界面

 

        修改界面

 总结:写的第一个留言板,出了很多bug,好在github和csdn足够强大,有问题欢迎指出!

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值