PHP+MySql实现简单的留言板功能-php教程

资源魔 42 0

【相干学习保举: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" 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['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" 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['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" 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 php开发教程 php开发资料 php开发自学 留言板

抱歉,评论功能暂时关闭!