Home Backend Development PHP Tutorial PHP+MySQL Make a Simple Guestbook_PHP Tutorial

PHP+MySQL Make a Simple Guestbook_PHP Tutorial

Jul 21, 2016 pm 03:42 PM
php+mysql main example Pagination make Function and reply increase show Compare message of Simple page

Message display page: Compared with the previous example, the functions of paging and message reply are added

Main code:
install/index.php: program installation page

Copy code The code is as follows:

if($_GET["action"]!=1)
{
?>






































MySQL主机名:
MySQL用户名:
MySQL密码:
数据库名称:
留言本每页记录数:
管理员账号:
管理员密码:
留言本标题:


}else{
@set_time_limit(1000);
$lockfile = "install.lock";
$host="localhost";
$user="root";
$password="";
$database="ruizhi_messageboard";
$pagenum=10;
$admin_name="admin";
$admin_password="123456";
$boardname="RUIZHINET留言本";
if($_POST["host"]!=""){
$host=$_POST["host"];
}
if($_POST["user"]!=""){
$user=$_POST["user"];
}
if($_POST["password"]!=""){
$password=$_POST["password"];
}
if($_POST["database"]!=""){
$database=$_POST["database"];
}
if($_POST["pagenum"]!=""){
$pagenum=$_POST["pagenum"];
}
if($_POST["admin_name"]!=""){
$admin_name=$_POST["admin_name"];
}
if($_POST["admin_password"]!=""){
$admin_password=$_POST["admin_password"];
}
if($_POST["boardname"]!=""){
$boardname=$_POST["boardname"];
}
if(file_exists($lockfile)){
exit("已经安装过了,如果要重新安装请先删除install/install.lock");
}
$conn=mysql_connect($host,$user,$password);
if($conn){
$sql_drop_database="DROP DATABASE IF EXISTS `".$database."`";
$sql_create_database="CREATE DATABASE `".$database."`";
$sql_create_table_messageboard="CREATE TABLE `messageboard` (
`id` int(11) NOT NULL auto_increment,
`author` varchar(15) NOT NULL,
`title` varchar(30) NOT NULL,
`smiles` varchar(30) NOT NULL,
`content` mediumtext,
`photo` varchar(30) NOT NULL,
`addtime` int(11) default NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=gbk AUTO_INCREMENT=1";
$sql_create_table_reply="CREATE TABLE `reply` (
`id` int(11) NOT NULL auto_increment,
`msgid` int(11) NOT NULL,
`content` mediumtext,
`addtime` int(11) default NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=gbk AUTO_INCREMENT=1";
if(mysql_query($sql_drop_database,$conn)){
if(mysql_query($sql_create_database,$conn)){
mysql_select_db($database,$conn);
if(mysql_query($sql_create_table_messageboard,$conn) && mysql_query($sql_create_table_reply,$conn)){
$config_file="../config.php";
$config_strings="$config_strings.="$boardname="".$boardname."";n";
$config_strings.="$host="".$host."";n";
$config_strings.="$user="".$user."";n";
$config_strings.="$password="".$password."";n";
$config_strings.="$database="".$database."";n";
$config_strings.="$pagenum="".$pagenum."";n";
$config_strings.="$admin_name="".$admin_name."";n";
$config_strings.="$admin_password="".$admin_password."";n";
$config_strings.="$conn=mysql_connect($host,$user,$password);n";
$config_strings.="mysql_select_db($database,$conn);n";
$config_strings.="?>";
if($fp=fopen($config_file,"wb")){
if(fwrite($fp,$config_strings)){
if($fp2 = fopen($lockfile, 'w'))
{
fwrite($fp2,'1212');
fclose($fp2);
}
echo "安装成功!配置文件为:config.php,您可以手工修改该文件";
echo "n进入系统首页";
}else{
exit("文件写入失败");
}
fclose($fp);
}
}else{
exit("不能执行CREATE TABLE语句:".$sql_create_table);
}
}else{
exit("不能执行CREATE DATABASE语句:".$sql_create_database);
}
}else{
exit("不能执行DROP DATABASE语句:".$sql_drop_database);
}
}else{
exit("连接数据库失败,请检查MySQL主机名、用户名和密码");
}
}

生成的config.php文件:
复制代码 代码如下:

$boardname="RUIZHINET留言本";
$host="localhost";
$user="root";
$password="";
$database="ruizhi_messageboard";
$pagenum="10";
$admin_name="admin";
$admin_password="admin";
$conn=mysql_connect($host,$user,$password);
mysql_select_db($database,$conn);
?>

index.php:程序显示页面
复制代码 代码如下:

if(!$_GET["page"]){
$page=1;
}else{
$page=$_GET["page"];
}
$sql="SELECT id FROM messageboard";
$result=mysql_query($sql,$conn);
$row_num=mysql_num_rows($result);
$page_count=ceil($row_num/$pagenum);
echo "当前共有".$row_num."条留言";
echo "  共分".$page_count."页显示";
echo "  当前为第".$page."页";
?>


if($row_num==0){
echo "暂时没有留言";
}else{
?>


$offset=($page-1)*$pagenum;
$sql2="SELECT * FROM messageboard ORDER BY id DESC LIMIT $offset,$pagenum";
$result2=mysql_query($sql2,$conn);
while($row=mysql_fetch_array($result2)){
$offset++;
echo "";
echo "";
echo "";
if($row["photo"]!="NONE")
{
echo "";
}
$sql3="SELECT * FROM reply WHERE msgid=$row[id] ORDER BY id DESC";
$result3=mysql_query($sql3,$conn);
while($row3=mysql_fetch_array($result3)){
echo "";
echo "";
}
}
?>
if($page_count>1){
$prev_page=$page-1;
$next_page=$page+1;
if($page<=1){
echo "第一页 | ";
}else{
echo "第一页 | ";
}
if($prev_page<1){
echo "上一页 | ";
}else{
echo "上一页 | ";
}
if($next_page>$page_count){
echo "下一页 | ";
}else{
echo "下一页 | ";
}
if($page>=$page_count){
echo "最后一页";
}else{
echo "最后一页";
}
}
?>
";
echo $offset.".";
echo base64_decode($row[title])." - ".base64_decode($row[author])." [".date("Y年m月d日",$row[addtime])."] ";
if(isset($_SESSION["admin_name"]) && $_SESSION["admin_name"]!=""){
echo "删除 | ";
echo "回复";
}
echo "
".base64_decode($row["content"])."
回复:".date("Y年m月d日",$row3[addtime]);
if(isset($_SESSION["admin_name"]) && $_SESSION["admin_name"]!=""){
echo "删除";
}
echo "
".base64_decode($row3[content])."

}
?>
if(isset($_SESSION["admin_name"]) && $_SESSION["admin_name"]!=""){
?>

退出管理


}else{
?>

登陆管理


}
?>

saveadd.php:保存留言
复制代码 代码如下:

if(!$_POST["author"] || !$_POST["content"])
{
echo "n";
echo "你没有填写留言姓名或内容,2秒钟返回首页";
exit();
}else{
$imgflag=0;
function random($length)
{
$hash = 'IMG-';
$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';
$max = strlen($chars) - 1;
echo (double)microtime() * 1000000;
for($i = 0; $i < $length; $i++)
{
$hash .= $chars[mt_rand(0, $max)];
}
return $hash;
}
function fileext($filename)
{
return substr(strrchr($filename, '.'), 1);
}
if($_FILES["upfile"]["name"]!=""){
$uploaddir="upfile/";
$type=array("jpg","gif","bmp","jpeg","png");
if(!in_array(strtolower(fileext($_FILES['upfile']['name'])),$type))
{
echo "n";
$text=implode(",",$type);
echo "您只能上传以下类型文件: ",$text,"
";
exit();
}
else
{
$filename=explode(".",$_FILES['upfile']['name']);
do
{
$filename[0]=random(10);
$randname=implode(".",$filename);
$uploadfile=$uploaddir.$randname;
} while(file_exists($uploadfile));
if (move_uploaded_file($_FILES['upfile']['tmp_name'],$uploadfile)){
echo "上传图片成功";
$imgflag=1;
}
else{
echo "上传图片失败!";
$imgflag=0;
}
}
}
$author=base64_encode($_POST["author"]);
$content=base64_encode(ereg_replace("rn","
",htmlspecialchars($_POST["content"])));
$smiles=base64_encode($_POST["smiles"]);
if($_POST["title"]){
$title=base64_encode($_POST["title"]);
}else{
$title=base64_encode("无标题");
}
$addtime=time();
if($imgflag==1){
$photo=base64_encode($randname);
}else{
$photo="NONE";
}
//保存数据
$sql="INSERT INTO messageboard(author,title,smiles,content,photo,addtime) VALUES('$author','$title','$smiles','$content','$photo',$addtime)";
mysql_query($sql,$conn);
echo "n";
echo "谢谢您的留言,2秒钟返回首页";
}
?>
savereply.php:保存回复
if(isset($_SESSION["admin_name"]) && $_SESSION["admin_name"]!="")
{
if(!$_POST["content"])
{
echo "n";
echo "你没有填写回复内容,2秒钟返回首页";
exit();
}else{
$msgid=$_POST["msgid"];
$content=base64_encode(ereg_replace("rn","
",htmlspecialchars($_POST["content"])));
$addtime=time();
//保存数据
$sql="INSERT INTO reply(msgid,content,addtime) VALUES($msgid,'$content',$addtime)";
mysql_query($sql,$conn);
echo "n";
echo "谢谢您的留言,2秒钟返回首页";
}
}
?>

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/320898.htmlTechArticle留言显示页面:比上一个例子增加了分页和留言回复的功能 主要代码: install/index.php:程序安装页面 复制代码 代码如下: ?php if($_GET["acti...
Statement of this Website
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

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

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Use ddrescue to recover data on Linux Use ddrescue to recover data on Linux Mar 20, 2024 pm 01:37 PM

DDREASE is a tool for recovering data from file or block devices such as hard drives, SSDs, RAM disks, CDs, DVDs and USB storage devices. It copies data from one block device to another, leaving corrupted data blocks behind and moving only good data blocks. ddreasue is a powerful recovery tool that is fully automated as it does not require any interference during recovery operations. Additionally, thanks to the ddasue map file, it can be stopped and resumed at any time. Other key features of DDREASE are as follows: It does not overwrite recovered data but fills the gaps in case of iterative recovery. However, it can be truncated if the tool is instructed to do so explicitly. Recover data from multiple files or blocks to a single

The difference between vivox100s and x100: performance comparison and function analysis The difference between vivox100s and x100: performance comparison and function analysis Mar 23, 2024 pm 10:27 PM

Both vivox100s and x100 mobile phones are representative models in vivo's mobile phone product line. They respectively represent vivo's high-end technology level in different time periods. Therefore, the two mobile phones have certain differences in design, performance and functions. This article will conduct a detailed comparison between these two mobile phones in terms of performance comparison and function analysis to help consumers better choose the mobile phone that suits them. First, let’s look at the performance comparison between vivox100s and x100. vivox100s is equipped with the latest

What exactly is self-media? What are its main features and functions? What exactly is self-media? What are its main features and functions? Mar 21, 2024 pm 08:21 PM

With the rapid development of the Internet, the concept of self-media has become deeply rooted in people's hearts. So, what exactly is self-media? What are its main features and functions? Next, we will explore these issues one by one. 1. What exactly is self-media? We-media, as the name suggests, means you are the media. It refers to an information carrier through which individuals or teams can independently create, edit, publish and disseminate content through the Internet platform. Different from traditional media, such as newspapers, television, radio, etc., self-media is more interactive and personalized, allowing everyone to become a producer and disseminator of information. 2. What are the main features and functions of self-media? 1. Low threshold: The rise of self-media has lowered the threshold for entering the media industry. Cumbersome equipment and professional teams are no longer needed.

The original comment was deleted. Is the reply still there? Can others still see my comments after I delete them? The original comment was deleted. Is the reply still there? Can others still see my comments after I delete them? Mar 23, 2024 pm 05:56 PM

In a world where social media is overflowing with information, people are paying more and more attention to the content they post on the platform and how they interact with others. When we leave a comment under a certain post, if the original author deletes it, whether the comment will continue to exist has become a hotly debated issue. 1. The original comment has been deleted. Is the reply still there? First, it needs to be clear that social media platforms are highly flexible in how they handle user information and interactions. Even though the original comment is deleted, replies often remain below the post, even though they may not appear to be directly connected. This means that even if the original comment has disappeared, subsequent readers can still see the replies and infer some information based on those replies. Therefore, deleting the original comment will not completely eliminate traces of the interaction.

What are the functions of Xiaohongshu account management software? How to operate a Xiaohongshu account? What are the functions of Xiaohongshu account management software? How to operate a Xiaohongshu account? Mar 21, 2024 pm 04:16 PM

As Xiaohongshu becomes popular among young people, more and more people are beginning to use this platform to share various aspects of their experiences and life insights. How to effectively manage multiple Xiaohongshu accounts has become a key issue. In this article, we will discuss some of the features of Xiaohongshu account management software and explore how to better manage your Xiaohongshu account. As social media grows, many people find themselves needing to manage multiple social accounts. This is also a challenge for Xiaohongshu users. Some Xiaohongshu account management software can help users manage multiple accounts more easily, including automatic content publishing, scheduled publishing, data analysis and other functions. Through these tools, users can manage their accounts more efficiently and increase their account exposure and attention. In addition, Xiaohongshu account management software has

How to leave a message on Xianyu How to leave a message on Xianyu and chat privately How to leave a message on Xianyu How to leave a message on Xianyu and chat privately Mar 23, 2024 am 09:10 AM

Xianyu APP is a super second-hand trading software. It allows everyone to choose products here at will, or publish their own products for sale. There is no problem. Everything can be realized here. When you use it here When using the Xianyu APP, everyone needs to communicate in a timely manner by leaving messages to get more information and help you sell or buy goods better. It is relatively convenient and is aimed at those who still don’t know how to leave messages on Xianyu. For my friends, I have now brought you specific Xianyu message tutorials. I hope it will be helpful to you. Xianyu message tutorial: 1. First, open Xianyu. 2. Then enter the interface and click on a product. 3. Then in the interface that appears, slide up and click

Specific method to create film movement effect in PPT Specific method to create film movement effect in PPT Mar 26, 2024 pm 04:00 PM

1. Start PPT, create a new blank document, select all text boxes and delete them. 2. Execute the Insert-Shape command, drag a rectangle in the document, and fill the shape with black. 3. Drag the rectangle to elongate it, execute the Insert-Shape command, drag out the small square, and set the fill color to white. 4. Copy and paste the small squares one by one so that the top and bottom are evenly distributed on both sides of the film. After selecting them all with ctrl+a, right-click and select Group. 5. Execute the Insert-Picture command, find the picture to be inserted in the pop-up dialog box, click to open, and adjust the size and position of the picture. 6. Repeat step 5 to insert and set the remaining pictures in order to form a film picture. 7. Select the film, execute animation-add animation command

How to write an interesting reply sentence to a TikTok comment? What are the benefits of replying to comments? How to write an interesting reply sentence to a TikTok comment? What are the benefits of replying to comments? Mar 21, 2024 pm 06:01 PM

On this platform, in addition to watching interesting short videos, browsing interesting comments has also become an enjoyable experience for many users. Funny Tiktok comment replies not only elicit laughter but also resonate, and sometimes add sparkle to the content. 1. How to write an interesting reply sentence to a Douyin comment? 1. Integrate current affairs hot spots: Current affairs hot spots are the focus of everyone's attention. Integrating them into comments and replies can quickly arouse the interest of others. For example, on a popular dance video on Douyin, you can comment: &quot;Is this the 'social shake' that became popular during the epidemic in my country? It's so energetic!&quot; Such comments are humorous and appropriate, and can make people understand My heart smiles. The use of exaggeration is a common technique in humorous commentary. By moderately exaggerating an object or situation, you can make the review more interesting and make the

See all articles