搜索
首页后端开发php教程PHP+MySql实现简单的留言板功能

PHP+MySql实现简单的留言板功能

Aug 27, 2020 pm 04:37 PM
php留言板

PHP+MySql实现简单的留言板功能

【相关学习推荐: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(&#39;$name&#39;,&#39;$addtime&#39;,&#39;$content&#39;,&#39;&#39;)";
    mysqli_query($link,$insert);
    mysqli_close($link);
    echo "<script language=javascript>alert(&#39;留言成功!单击确定查看留言.&#39;);location.href=&#39;index.php&#39;;</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" rel="external nofollow" rel="external nofollow" rel="external nofollow" >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[&#39;author&#39;]."]于".$info[&#39;addtime&#39;]."说:<br>";
              echo "  ".$info[&#39;content&#39;]."<br>";
              if( $info[&#39;reply&#39;] != ""){
                // <b></b>显示粗体
                echo "<b>管理员回复:</b>".$info[&#39;reply&#39;]."<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" rel="external nofollow" rel="external nofollow" rel="external nofollow" >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[&#39;password&#39;];
    $link = mysqli_connect("127.0.0.1","root","Vmorish");
    mysqli_select_db($link,"gbook");
    $query = "select * from admin where username = &#39;$name&#39;";
    $result = mysqli_query($link,$query);
    if( mysqli_num_rows($result) < 1){
      echo "该用户不存在,请重新登录!<br>";
    }else{
      $info = mysqli_fetch_array($result);
      if( $info[&#39;userpass&#39;] != $password){
        echo "密码输入错误,请重新登录!<br>";
      }else{
        //如果用户名密码都正确,则注册一个session来标记其登录状态
        echo "hhhh<br>";
        session_start();
        // $_SESSION["login"] = "YES";
        echo "<script language=javascript>alert(&#39;登录成功!&#39;);location.href=&#39;manage.php&#39;;</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" rel="external nofollow" rel="external nofollow" rel="external nofollow" >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;
 ?>

相关学习推荐:php编程(视频)

以上是PHP+MySql实现简单的留言板功能的详细内容。更多信息请关注PHP中文网其他相关文章!

声明
本文转载于:jb51。如有侵权,请联系admin@php.cn删除
PHP和Python:解释了不同的范例PHP和Python:解释了不同的范例Apr 18, 2025 am 12:26 AM

PHP主要是过程式编程,但也支持面向对象编程(OOP);Python支持多种范式,包括OOP、函数式和过程式编程。PHP适合web开发,Python适用于多种应用,如数据分析和机器学习。

PHP和Python:深入了解他们的历史PHP和Python:深入了解他们的历史Apr 18, 2025 am 12:25 AM

PHP起源于1994年,由RasmusLerdorf开发,最初用于跟踪网站访问者,逐渐演变为服务器端脚本语言,广泛应用于网页开发。Python由GuidovanRossum于1980年代末开发,1991年首次发布,强调代码可读性和简洁性,适用于科学计算、数据分析等领域。

在PHP和Python之间进行选择:指南在PHP和Python之间进行选择:指南Apr 18, 2025 am 12:24 AM

PHP适合网页开发和快速原型开发,Python适用于数据科学和机器学习。1.PHP用于动态网页开发,语法简单,适合快速开发。2.Python语法简洁,适用于多领域,库生态系统强大。

PHP和框架:现代化语言PHP和框架:现代化语言Apr 18, 2025 am 12:14 AM

PHP在现代化进程中仍然重要,因为它支持大量网站和应用,并通过框架适应开发需求。1.PHP7提升了性能并引入了新功能。2.现代框架如Laravel、Symfony和CodeIgniter简化开发,提高代码质量。3.性能优化和最佳实践进一步提升应用效率。

PHP的影响:网络开发及以后PHP的影响:网络开发及以后Apr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP类型提示如何起作用,包括标量类型,返回类型,联合类型和无效类型?PHP类型提示如何起作用,包括标量类型,返回类型,联合类型和无效类型?Apr 17, 2025 am 12:25 AM

PHP类型提示提升代码质量和可读性。1)标量类型提示:自PHP7.0起,允许在函数参数中指定基本数据类型,如int、float等。2)返回类型提示:确保函数返回值类型的一致性。3)联合类型提示:自PHP8.0起,允许在函数参数或返回值中指定多个类型。4)可空类型提示:允许包含null值,处理可能返回空值的函数。

PHP如何处理对象克隆(克隆关键字)和__clone魔法方法?PHP如何处理对象克隆(克隆关键字)和__clone魔法方法?Apr 17, 2025 am 12:24 AM

PHP中使用clone关键字创建对象副本,并通过\_\_clone魔法方法定制克隆行为。1.使用clone关键字进行浅拷贝,克隆对象的属性但不克隆对象属性内的对象。2.通过\_\_clone方法可以深拷贝嵌套对象,避免浅拷贝问题。3.注意避免克隆中的循环引用和性能问题,优化克隆操作以提高效率。

PHP与Python:用例和应用程序PHP与Python:用例和应用程序Apr 17, 2025 am 12:23 AM

PHP适用于Web开发和内容管理系统,Python适合数据科学、机器学习和自动化脚本。1.PHP在构建快速、可扩展的网站和应用程序方面表现出色,常用于WordPress等CMS。2.Python在数据科学和机器学习领域表现卓越,拥有丰富的库如NumPy和TensorFlow。

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
1 个月前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
1 个月前By尊渡假赌尊渡假赌尊渡假赌
威尔R.E.P.O.有交叉游戏吗?
1 个月前By尊渡假赌尊渡假赌尊渡假赌

热工具

VSCode Windows 64位 下载

VSCode Windows 64位 下载

微软推出的免费、功能强大的一款IDE编辑器

螳螂BT

螳螂BT

Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

功能强大的PHP集成开发环境

Dreamweaver Mac版

Dreamweaver Mac版

视觉化网页开发工具

MinGW - 适用于 Windows 的极简 GNU

MinGW - 适用于 Windows 的极简 GNU

这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。