PHP+MySql实现一个简单的留言板

//嗯,跟着书学的,代码不是自己写的,但是都能理解,有时间自己去写个好看一点的吼吼吼~(不熟练花了一天的时间…

留言板是接触WEB开发的基础,写一个留言板需要知道前端的一些基础标签,对数据库有一个了解会基础SQL语言,PHP基础知识,前段基础+数据库基础+PHP基础=>留言板。

前方高能哇(界面真的是吃藕诶…

先建一个数据库,数据库里有两张表,一个存账号密码,一个存留言信息

//创建数据库,里面有两张表Admin和Message
create database gbook;
//创建Admin表,记录用户名和密码
create table admin(
    username varchar(20) not null,
    userpass varchar(20) not null
);
//创建Message表,记录留言的id,留言人,留言日期,留言内容以及回复
create table message(
    id int(4) not null auto_increment primary key,
    author varchar(20) not null,
    addtime datetime not null,
    content varchar(1000) not null,
    reply varchar(1000) not null
);

首先实现用户留言的部分,这是第一步,没有留言index页面就空了嘛~

<!-- 1.用户填写留言部分 send.php -->
<!-- 可以首先编写send页面,只有用户提交了留言才能进行后面的留言显示,留言管理等等 -->

<?php
    $name = $_POST["name"];//从input里面传过来的name
    //看用户是否提交了新留言,如果提交了,则写入表message
    if( $name != ""){
        $content = $_POST["content"];
        //下面的代码用于获得当前日期和时间
        $addtime = date("Y-m-d h:i:s");//得到日期
        $link = mysqli_connect("127.0.0.1","root","Vmorish");//PHP连接数据库
        if( $link)
            echo "ok!<br>";
        else {
            echo "bad!<br>";
        }
        mysqli_select_db($link,"gbook");//选择数据库
        $insert = "insert into message(author,addtime,content,reply) values('$name','$addtime','$content','')";
        mysqli_query($link,$insert);
        mysqli_close($link);
        echo "<script language=javascript>alert('留言成功!单击确定查看留言.');location.href='index.php';</script>";
    }
    mysqli_close($link);

 ?>

<html>

<head>
    <title>欢迎来到陈雨情的留言本吼吼吼</title>
</head>

<body>
    <!-- border-collapse:collapse合并表格的边框 -->
    <table border=1 cellspacing=0 cellspadding=0 style="border-collapse:collapse" align=center width=400 bordercolor=black>
        <tr>
            <td height=100 bgcolor=#6c6c6c>
                <font style="font-size:30px" color=#ffffff face="黑体">欢迎来到×××的留言本吼吼吼</font>
            </td>
        </tr>
        <tr>
            <td height=25>
                 <a href=send.php>[我要写留言]</a> 
                 <a href=login.php>[管理留言]</a>
            </td>
        </tr>
        <tr>
            <td height=200>
                <form method="POST" action="send.php">
                    <table border="1" width="95%" id="table1" cellspacing="0" cellpadding="0" bordercolor="#808080" style="border-collapse:collapse" height="265">
                        <tr>
                            <td colspan="2" height="29">
                                <p align="center">欢迎填写你的留言</p>
                            </td>
                        </tr>
                        <tr>
                            <td width="32%">
                                <p align="right">你的名字</p>
                            </td>
                            <td width="67%">
                                <input type="text" name="name" size="20">
                            </td>
                        </tr>
                        <tr>
                            <td width="32%">
                                <p>留言内容</p>
                            </td>
                            <td width="67%">
                                <textarea rows="10" name="content" cols="31"></textarea>
                            </td>
                        </tr>
                        <tr>
                            <td width="99%" colspan="2">
                                <p align="center">
                                    <input type="submit" value="提交" name="B1">
                                </p>
                            </td>
                        </tr>
                    </table>
                </form>
            </td>
        </tr>
        <tr>
            <td height=80 bgcolor=#6c6c6c align=center>
                <font color="#FFFFFF">
                    版权所有:<a href="http://blog.csdn.net/cherish0222">Vmorish</a><br>
                    E-mail:vmorish@163.com
                </font>
            </td>
        </tr>
    </table>

</body>

</html>

效果:


接着就可以上主页面了

<!-- 2.留言本首页 index.php -->
<!-- 本页面显示十条最近的的留言,并且有分页功能 -->
<html>

<head>
    <title>欢迎来到陈雨情的留言本吼吼吼</title>
    <style type="text/css">
        TD{
            font-size: 12px;
            line-height: 150%;
        }
    </style>
</head>

<body>
    <table border=1 cellspacing=0 cellspadding=0 style="border-collapse:collapse" align=center width=400 bordercolor=black height=382>
        <tr>
            <td height=100 bgcolor=#6c6c6c style="font-size:30px;line-height:30px">
                <font color=#ffffff face="黑体">欢迎来到×××的留言本吼吼吼</font>
            </td>
        </tr>
        <tr>
            <td height=25>
                 <a href=send.php>[我要写留言]</a> 
                 <a href=login.php>[管理留言]</a>
            </td>
        </tr>
        <tr>
            <td height=200>
                <?php
                    $link = mysqli_connect("127.0.0.1","root","Vmorish");
                    mysqli_select_db($link,"gbook");
                    $query = "select * from message";
                    $result = mysqli_query($link,$query);
                    if( mysqli_num_rows($result) < 1){
                        echo " 目前数据表中还没有任何留言!";
                    }else{
                        $totalnum = mysqli_num_rows($result);//获取数据库中所有数据条数
                        $pagesize = 7;//每页显示7条
                        $page = $_GET["page"];
                        if( $page == ""){
                            $page = 1;
                        }
                        $begin = ($page-1)*$pagesize;
                        $totalpage = ceil($totalnum/$pagesize);
                        //输出分页信息
                        echo "<table border=0 width=95%><tr><td>";
                        $datanum = mysqli_num_rows($result);
                        echo "共有".$totalnum."条留言,每页".$pagesize."条,共".$totalpage."页。<br>";
                        //输出页码
                        for( $i = 1; $i <= $totalpage; $i++){
                            echo "<a href=index.php?page=".$i.">[".$i."] </a>";
                        }
                        echo "<br>";
                        //从message表中查询当前页面所要显示的留言,并根据时间排序
                        $query = "select * from message order by addtime desc limit $begin,$pagesize";
                        $result = mysqli_query($link,$query);
                        $datanum = mysqli_num_rows($result);
                        //循环输出所有留言,如果管理员已经回复则同时输出回复
                        for( $i = 1; $i <= $datanum; $i++){//$datanum???
                            $info = mysqli_fetch_array($result);
                            echo "->[".$info['author']."]于".$info['addtime']."说:<br>";
                            echo "  ".$info['content']."<br>";
                            if( $info['reply'] != ""){
                                // <b></b>显示粗体
                                echo "<b>管理员回复:</b>".$info['reply']."<br>";
                            }
                            echo "<hr>";
                        }//else结束
                        echo "</td></tr></table>";
                    }
                    mysqli_close($link)
                 ?>
            </td>
        </tr>
        <tr>
            <td height=80 bgcolor=#6c6c6c align=center>
                <font color="#FFFFFF">
                    版权所有:<a href="http://blog.csdn.net/cherish0222">Vmorish</a><br>
                    E-mail:vmorish@163.com
                </font>
            </td>
        </tr>
    </table>

</body>

</html>


效果:


接着管理员登录咯

<!-- 3.管理员登录页面 login.php -->
<!-- 供管理员登录 -->
<!-- 体会session实现用户登录的方法 -->

<?php
    $name = $_POST["name"];
    if( $name != ""){
        $password = $_POST['password'];
        $link = mysqli_connect("127.0.0.1","root","Vmorish");
        mysqli_select_db($link,"gbook");
        $query = "select * from admin where username = '$name'";
        $result = mysqli_query($link,$query);
        if( mysqli_num_rows($result) < 1){
            echo "该用户不存在,请重新登录!<br>";
        }else{
            $info = mysqli_fetch_array($result);
            if( $info['userpass'] != $password){
                echo "密码输入错误,请重新登录!<br>";
            }else{
                //如果用户名密码都正确,则注册一个session来标记其登录状态
                echo "hhhh<br>";
                session_start();
                // $_SESSION["login"] = "YES";
                echo "<script language=javascript>alert('登录成功!');location.href='manage.php';</script>";
            }
        }
        mysqli_close($link);
    }
 ?>

<html>

<head>
    <title>欢迎来到陈雨情的留言本吼吼吼</title>
</heda>

<body>

    <table border=1 cellspacing=0 cellspadding=0 style="border-collapse:collapse" align=center width=400 bordercolor=black height="358">
        <tr>
            <td height=100 bgcolor=#6c6c6c style="font-size:30px;line-height:30px">
                <font color=#ffffff face="黑体">欢迎来到×××的留言本吼吼吼</font>
            </td>
        </tr>
        <tr>
            <td height=25>
                 <a href=send.php>[我要写留言]</a> 
                 <a href=login.php>[管理留言]</a>
            </td>
        </tr>
        <tr>
            <td height=178>
                <form method="POST" action="login.php">
                    <table border="1" width="95%" id="table1" cellspcing="0" cellpadding="0" bordercolor="#808080" style="border-collapse" height="154">
                        <tr>
                            <td colspan="2" height="29">
                                <p align="center">欢迎管理员登录</p>
                            </td>
                        </tr>
                        <tr>
                            <td width="32%">
                                <p align="center">用户名</P>
                            </td>
                            <td width="67%">
                                <input type="text" name="name" size="20">
                            </td>
                        </tr>
                        <tr>
                            <td width="32%">
                                <p align="center">密 码</p>
                            </td>
                            <td>
                                <input type="password" name="password" size="20">
                            </td>
                        </tr>
                        <tr>
                            <td width="99%" colspan="2">
                                <p align="center"><input type="submit" value="登录" name="B1"></p>
                            </td>
                        </tr>
                    </table>
                </form>
            </td>
        </tr>
        <tr>
            <td height=80 bgcolor=#6c6c6c align=center>
                <font color="#FFFFFF">
                    版权所有:<a href="http://blog.csdn.net/cherish0222">Vmorish</a><br>
                    E-mail:vmorish@163.com
                </font>
            </td>
        </tr>
    </table>

</body>

</html>

效果:


manage.php和reply.php和前面类似,就不给出了(我也还没写好诶…但要实现的跟前面类似

最后注销登录

<!-- 6.注销登录页面 -->
<?php
    session_start();
    $_SESSION["login"]="";
    echo "已成功退出。[<a href=index.php>回首页</a>]";
    exit;
 ?>


转载请注明出处:http://blog.csdn.net/cherish0222/article/details/77993700
  • 31
    点赞
  • 222
    收藏
    觉得还不错? 一键收藏
  • 10
    评论
好的,下面是一个简单留言板示例,使用 PHPMySQL 实现: 1. 创建一个名为 "messages" 的 MySQL 数据库,其中包含一个名为 "messages" 的表,该表包含以下字段:id(自动递增的主键)、username、email、message 和 created_at(时间戳)。 2. 创建一个 index.php 文件来显示留言板页面,并包含一个表单以允许用户提交新的留言。 ```php <!DOCTYPE html> <html> <head> <title>留言板</title> </head> <body> <h1>留言板</h1> <form action="submit.php" method="POST"> <label for="username">用户名:</label> <input type="text" id="username" name="username"><br> <label for="email">邮箱:</label> <input type="email" id="email" name="email"><br> <label for="message">留言:</label> <textarea id="message" name="message"></textarea><br> <input type="submit" value="提交"> </form> </body> </html> ``` 3. 创建一个 submit.php 文件来处理表单提交,并将数据插入到数据库中。 ```php <?php // 连接到 MySQL 数据库 $mysqli = new mysqli("localhost", "username", "password", "messages"); // 检查连接是否成功 if ($mysqli->connect_error) { die("连接失败: " . $mysqli->connect_error); } // 获取表单提交的数据 $username = $_POST["username"]; $email = $_POST["email"]; $message = $_POST["message"]; // 插入数据到数据库中 $sql = "INSERT INTO messages (username, email, message, created_at) VALUES ('$username', '$email', '$message', NOW())"; if ($mysqli->query($sql) === TRUE) { // 成功插入数据,重定向到留言板页面 header("Location: index.php"); exit; } else { // 插入数据失败,输出错误信息 echo "Error: " . $sql . "<br>" . $mysqli->error; } // 关闭数据库连接 $mysqli->close(); ?> ``` 4. 创建一个 messages.php 文件来显示所有留言。 ```php <!DOCTYPE html> <html> <head> <title>留言板</title> </head> <body> <h1>留言板</h1> <a href="index.php">添加新留言</a> <?php // 连接到 MySQL 数据库 $mysqli = new mysqli("localhost", "username", "password", "messages"); // 检查连接是否成功 if ($mysqli->connect_error) { die("连接失败: " . $mysqli->connect_error); } // 查询所有留言 $sql = "SELECT * FROM messages ORDER BY created_at DESC"; $result = $mysqli->query($sql); if ($result->num_rows > 0) { // 输出每个留言 while($row = $result->fetch_assoc()) { echo "<div>"; echo "<p><strong>用户名:</strong>" . $row["username"] . "</p>"; echo "<p><strong>邮箱:</strong>" . $row["email"] . "</p>"; echo "<p><strong>留言:</strong>" . $row["message"] . "</p>"; echo "<p><strong>时间:</strong>" . date("Y-m-d H:i:s", strtotime($row["created_at"])) . "</p>"; echo "</div>"; } } else { echo "没有留言。"; } // 关闭数据库连接 $mysqli->close(); ?> </body> </html> ``` 这样就完成了一个简单留言板,用户可以提交新的留言并在 messages.php 页面上查看所有留言。请注意,此示例仅用于教学目的,并不适用于生产环境。在实际应用中,您应该使用更安全的方法来处理用户输入(例如使用预处理语句)以及验证和过滤数据。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值