search
HomeheadlinesUse PHP to implement low-profile guestbook function code display

This article mainly introduces the implementation code of PHP simple guestbook function in detail. It has certain reference value. Interested friends can refer to it.

The example in this article shares the PHP message with everyone. The specific code of this function is for your reference. The specific content is as follows

index.php

<?php   
  error_reporting(0); //关闭NOTICE提示
  require_once "conn.php";                     
  $pagesize=5;   //每页显示5条数据
  $sql="select count(*) from guestlist "; //选择数据库,计算符合条件的行数并返回行数
  $result= mysql_query($sql);  //执行,如果成功则返回结果集(从数据库中找到所有的数据,返回条数)
  $row = mysql_fetch_row($result);  //获得数组 Array[0]="数据库里的总条数"
  $infoCount =$row[0]; //获得总条数:取得数组中的值$row[0]="数据库里的总条数"                       
  $pageCount = ceil($infoCount/$pagesize); //获取总页数(总个数/每页的个数5)
  $currpage=empty ($_GET["page"])?1:$_GET["page"]; //如果当前页为空 则定义page=1即$currpage=1反之亦然
  if($currpage>$pageCount)  //如果输入的页数超过总页数则默认跳转到最后一页
  {
   $currpage=$pageCount;
  }  
?>
<!DOCTYPE html>
<html>
 <head>
  <meta charset="utf-8" />
  <title></title>
       <!--此处添加了bootstrip样式--> 
  <link href="../dist/css/bootstrap.min.css" rel="external nofollow" type="text/css" rel="stylesheet" />
  <link href="css/index.css" rel="external nofollow" type="text/css" rel="stylesheet" />
  <script>
   function test(){
    var sum;
    if(document.frm.title.value==&#39;&#39;){
     alert(&#39;请填写标题&#39;);
     return false;
    }else{
     sum =document.frm.title.value.length;
     if(sum<5 || sum>20){
      alert(&#39;标题长度 5-20个字符&#39;);
      return false;
     }
    }
    
    if(document.frm.username.value==&#39;&#39;){
     alert(&#39;请填写用户网名&#39;);
     return false;
    }
    
    if(document.frm.content.value==&#39;&#39;){
     alert("请填写内容");
     return false;
    }
   return true;
   }
  </script>
 </head>
 <body>
  <p class="content">
  <h5 id="php-nbsp-echo-nbsp-infoCount-条留言"><?php echo $infoCount;?>条留言</h5><br/>
  <ul class="bt">
   <li>留言标题</li>
   <li>用户网名</li>
   <li>时间</li>
  </ul> 
   <?php               //从当前页开始 向下取出5个
      $re= mysql_query("select * from guestlist order by id desc limit ".($currpage-1)*$pagesize.",".$pagesize);
     while($row= mysql_fetch_assoc($re)) //得到一行数据的数组,再执行则得到再下一行,如果得到是最后一行,那么再执行则返回false
     {
     
      ?>
        <ul class="nr">
         <li><?php echo $row["title"];?></li>
         <li><?php echo $row["username"];?></li>
         <li><?php echo $row["addtime"];?></li>
        </ul>
        <p class="lynr">
        <p><strong>留言内容:</strong></p><span><?php echo $row["content"];?></span>  
        </p> 
       <?php
     }
   ?>
   <hr style="width:800px"/>
   <ul class="pagination"> 
    <!--上一页-->
    <?php 
      for($i=1;$i<=$pageCount;$i++)
       {
        
        if($i==$currpage)            
         {
         echo "<li><a href=?page=".($i-1).">«</a></li>"; 
         }
       
       } 
    ?> 
    <!--数字页-->
    <?php 
    
      for($i=1;$i<=$pageCount;$i++)
       {
        
        if($i==$currpage)            
         {
         echo "<li ><a style=&#39;background-color:#EEEEEE&#39;>$i</a></li>"; 
         }else{                
         echo "<li><a href=&#39;?page=$i&#39;>$i</a></li>";} 
       
       } 
    ?> 
    <!--下一页-->
    <?php 
    
      for($i=1;$i<$pageCount;$i++)
       {
        
        if($i==$currpage)            
         {
         echo "<li><a href=?page=".($i+1).">»</a></li>"; 
         }
       
       } 
    ?> 
   </ul>
   <br/>
   <ul>
   </ul>
   <hr/>
   <strong style="color:red">发表留言</strong>
   <form action="result.php" method="post" name="frm" onsubmit="return test()">
   <table cellpadding="0" cellspacing="0" >
    <tr>
     <td >留言标题:</td>
     <td><input type="text" name="title" autocomplete="off"/></td>
    </tr>
    <tr>
     <td>网名:</td>
     <td><input type="text" name="username" autocomplete="off"/></td>
    </tr>
    <tr>
     <td>留言内容:</td>
     <td><textarea name="content" cols="42" rows="5" autocomplete="off"/></textarea></td>
    </tr>
    <tr>
     <td></td>
     <td><input class="btn" type="submit" name="submit" value="提交"/></td>
    </tr>
   </table>
   </form>
  </p> 
 </body>
</html>

conn.php

<?php
$link = mysql_connect("localhost","root"," ");
mysql_select_db("guestbook");
mysql_query("set names utf-8");
if(!$link){
 die("Connection failed: " . mysqli_connect_error());
}
 //echo "链接成功";
 
?>

result.php

<?php
 error_reporting(0);                          //关闭NOTICE提示
 require_once "conn.php";
 $title = $_REQUEST[&#39;title&#39;];
 $username = $_REQUEST[&#39;username&#39;];
 $content = $_REQUEST[&#39;content&#39;];
 $content = str_replace("\n","<br>",str_replace(" "," ",$content)); //显示&#39;空格&#39;和&#39;回车&#39;
 $week = &#39;星期&#39;.mb_substr( "日一二三四五六",date("w"),1,"utf-8" );
    $isok =mysql_query("insert into guestlist(title,username,content,addtime)values(&#39;$title&#39;,&#39;$username&#39;,&#39;$content&#39;,&#39;".date("Y-m-d H:i:s")." $week &#39;)"); 
 if($isok)
  {
    echo "<script>
      alert(&#39;提交成功&#39;);
     location.href=&#39;index.php&#39;;
     </script>"; 
  }else {
    echo "<script>
      alert(&#39;提交失败&#39;);
     location.href=&#39;index.php&#39;;
     </script>";
  } 
?>

css /index.css

body{margin:0;padding:0;}
ul,li{list-style: none;margin:0;padding:0;}
a{text-decoration: none;}
.content{
 width:800px;
 
 margin:0 auto;
 
}
.bt{
 width:799px;
 height:20px;
 text-align: center;
 background:#EB9316;
 margin:0 0 5px 0;
}
.bt>li{
 float:left;
 width:265px;
 height:20px;
 text-align: center;
 line-height: 20px;
 font-size:13px;
 
}
.nr{
 float:left;          /*如果不浮动 后面的lynr会受影响*/
 width:799px;
 height:20px;
 text-align: center;
 background:#B9DEF0;
}
.nr>li{
 float:left;
 width:265px;
 height:20px;
 text-align: center;
 line-height: 20px;
 font-size:13px;
 
}
.lynr{
 float:left;    /*如果不浮动会 布局会乱*/
 width:800px;
 margin:1px 0 1px 0;
 
}
.content p{
 width:70px;
 height:50px;
 float:left;
 
 
}
.content span{
 display: block;
 width:710px;
 float:left;
 
 
}

td{
 width:80px;
 padding:5px 0;
 /*border: 1px solid #79ABFE;*/
 }
td input,textarea{
 border: 1px solid #79ABFE;
}
/*tr{
 display:block;       /*将tr设置为块体元素 显示块状后 就将其包围住了 不是一个矩形了
 
 }*/

dist/css/bootstrap.min.css(Download by yourself)

Rendering:

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
php怎么把负数转为正整数php怎么把负数转为正整数Apr 19, 2022 pm 08:59 PM

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

php怎么实现几秒后执行一个函数php怎么实现几秒后执行一个函数Apr 24, 2022 pm 01:12 PM

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php怎么除以100保留两位小数php怎么除以100保留两位小数Apr 22, 2022 pm 06:23 PM

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

php怎么根据年月日判断是一年的第几天php怎么根据年月日判断是一年的第几天Apr 22, 2022 pm 05:02 PM

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php怎么判断有没有小数点php怎么判断有没有小数点Apr 20, 2022 pm 08:12 PM

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

php字符串有没有下标php字符串有没有下标Apr 24, 2022 am 11:49 AM

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php怎么替换nbsp空格符php怎么替换nbsp空格符Apr 24, 2022 pm 02:55 PM

方法:1、用“str_replace("&nbsp;","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\&nbsp\;||\xc2\xa0)/","其他字符",$str)”语句。

php怎么查找字符串是第几位php怎么查找字符串是第几位Apr 22, 2022 pm 06:48 PM

查找方法:1、用strpos(),语法“strpos("字符串值","查找子串")+1”;2、用stripos(),语法“strpos("字符串值","查找子串")+1”。因为字符串是从0开始计数的,因此两个函数获取的位置需要进行加1处理。

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

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 CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)