search
HomeBackend DevelopmentPHP TutorialPHP+MySql implements simple message board function

PHP+MySql implements simple message board function

[Related learning recommendations: mysql tutorial

I learned from the book. I didn’t write the code myself, but I can understand it if I have time. I’ll write a better-looking one myself~ (It took me a day if I’m not skilled...

Message board is the basis for WEB development. To write a message board, you need to know some basic tags on the front end, and have some knowledge about the database. A person who understands basic SQL language, basic knowledge of PHP, basic database basics and PHP basics => Message board.

The front is high energy wow (the interface is really confusing...

Build one first Database, there are two tables in the database, one stores the account password, and the other stores the message information

//创建数据库,里面有两张表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
);

First implement the user message part. This is the first step. If there is no message, the index page will be empty~

<!-- 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>

Effect:

Then you can go to the main page

<!-- 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>

Effect:

Then the administrator logs in

<!-- 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>

Effect:

manage.php and reply.php are similar to the previous ones, so they will not be given. (I haven’t written it yet...but the implementation is similar to the previous one

Finally log out and log in

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

Related learning recommendations: php programming (video)

The above is the detailed content of PHP+MySql implements simple message board function. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:jb51. If there is any infringement, please contact admin@php.cn delete
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

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

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools