Use 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==''){ alert('请填写标题'); return false; }else{ sum =document.frm.title.value.length; if(sum<5 || sum>20){ alert('标题长度 5-20个字符'); return false; } } if(document.frm.username.value==''){ alert('请填写用户网名'); return false; } if(document.frm.content.value==''){ alert("请填写内容"); return false; } return true; } </script> </head> <body> <p class="content"> <h5 style="color: red;"><?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='background-color:#EEEEEE'>$i</a></li>"; }else{ echo "<li><a href='?page=$i'>$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['title']; $username = $_REQUEST['username']; $content = $_REQUEST['content']; $content = str_replace("\n","<br>",str_replace(" "," ",$content)); //显示'空格'和'回车' $week = '星期'.mb_substr( "日一二三四五六",date("w"),1,"utf-8" ); $isok =mysql_query("insert into guestlist(title,username,content,addtime)values('$title','$username','$content','".date("Y-m-d H:i:s")." $week ')"); if($isok) { echo "<script> alert('提交成功'); location.href='index.php'; </script>"; }else { echo "<script> alert('提交失败'); location.href='index.php'; </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:

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.