Home Backend Development PHP Tutorial PHP implementation of common operation methods for text database examples_PHP tutorial

PHP implementation of common operation methods for text database examples_PHP tutorial

Jul 13, 2016 am 10:26 AM
php operate text database

PHP can realize five basic operations such as display, addition, modification, deletion and query of text database data.
Let's take a guestbook program as an example to briefly describe the five basic operations of PHP on data display, addition, modification, deletion, and query of text databases.

This text database has 10 fields in total: customer IP, speaking time, customer name, customer EMAIL, customer homepage address, message emoticon picture name, customer QQ, customer image picture, message content, and administrator reply content.

1. Add data program:

$date=date("Y-m-d H:i:s");//取得系统时间
$ip = $HTTP_SERVER_VARS[REMOTE_ADDR]; //取得发言的IP地址
$text=encode($gb_text);//去掉留言内容后面的空格.
$fp=fopen("gb.dat","a");//以只写模式打开gb.dat文本文件,文件指针指向文件尾部.
$str=$ip."|".$date."|".$gb_name."|".$gb_email."|".$gb_home."|".$face."|".$gb_qq."|".$head."|".$text."|".$reply." ";//将所有留言的数据赋予变量$str,"|"的目的是用来今后作数据分割时的数据间隔符号。
fwrite($fp,$str);//将数据写入文件
fclose($fp);//关闭文件
showmessage("留言成功!","index.php","3");//留言成功,3秒后自动返回主界面。

Copy after login

$gb_name, $gb_email, $gb_home, $face, $gb_qq, $head, $gb_text, $reply are the data passed from the speech form.

2. Display data program:

<&#63;
if (file_exists("gb.dat")){//检测文件是否存在
$array=file("gb.dat");//将文件全部内容读入到数组$array
$arr=array_reverse($array);//将$array里的数据安行翻转排列(即最后一行当第一行,依此类推)读入数组$arr的每一个单元($arr[0]...)。
$num=count($array);//获取数组$array里的信息数(一行为一条信息)
if ($num>0){//如果信息数大于零(即文本数据库不为空)
$total=ceil($num/$pagesize);//计算总页数(取最大整数,即凡有小数点都进一取整,$pagesize为预设的每页显示的信息数)
if($page<1){//如果当前页面数码号小于1
$page=1;//则赋值为1
}
$number=($page-1)*$pagesize;//计算当前所显示第一个留言的数码号(数码号从零开始,主要是达到与数组单元号对应的目的)
for($i=0;$i<=$pagesize-1;$i++){//进入循环
$row=explode("|",$arr[$number]);//以"|"作为分割符,分割数组$arr中每第$number个单元的数据,并将这些数据赋予数组$rom 
list($ip,$datetime,$name,$email,$home,$face,$qq,$head,$text,$reply)=$row;//将数组$row里的单元数据按顺序赋予括号里的变量
&#63;>
<img src=<&#63; echo $head &#63;> >//显示客户形象图片
<br>
<font color="#0099CC">昵称【<&#63; echo $name &#63;><font size="2">】<br>//显示客户名
发表于:<&#63; echo $datetime &#63;>//显示留言发表时间
<br> 
<img src=<&#63; echo $face &#63;>>//显示客户留言表情图片
<&#63; echo $name &#63;>说:<&#63; echo $text; &#63;>//显示客户留言内容 
<br> 
<&#63; echo $reply &#63;>//显示回复内容
<br>
<a href="<&#63; echo $home &#63;>" rel="external nofollow" target="_blank">访问<&#63; echo $name &#63;>的主页</a>//客户主页的超连接
<a href="mailto:<&#63; echo $email &#63;>" rel="external nofollow" >给<&#63; echo $name &#63;>发信</a>//客户E-MAIL的连接
<&#63; echo $name &#63;>的QQ号码是<&#63; echo $qq &#63;>//显示客户的QQ号码 
<&#63; echo $name &#63;>的IP地址为<&#63; echo $ip &#63;>" //显示客户的IP地址
<a href="reply.php&#63;time=<&#63; echo $datetime &#63;>" rel="external nofollow" >回复</a>//留言回复的连接语句 
<a href="del.php&#63;time=<&#63; echo $datetime &#63;>" rel="external nofollow" >删除</a>//留言删除的语句(以客户留言时间$datetime作为删除标识)
<br> 
<&#63; 
if ($number == $num-1)//如果数组的单元号等于总留言数减一(因为单元号以零开始的,所以这意味着这是最后一条留言)
{
break;//跳出循环
}
$number = $number + 1; //数组单元号加1
}//循环结束符
}
if ($page <> 1)//如果当前页面数码号不等于1
{
$back = $page - 1;//当前页面数码号减1,并将此值赋予变量$back
echo "<a href=index.php&#63;page=1>第一页</a>";//显示第一页的连接
echo " <a href=index.php&#63;page=$back>上一页</a>" ;当前页面数码号等于$back,并显示其连接
}
if ($page <> $total)//如果当前页面数码号不等于总页数号(最后一页数码号)
{
$next = $page + 1;//当前页面数码号加1并赋予变量$next
echo " <a href=index.php&#63;page=$next>下一页</a>" ;//显示下一页连接
echo " <a href=index.php&#63;page=$total>最后一页</a>"; 显示最后一页连接
} 
echo "页数:$page / $total";//显示当前页面数码号和显示最后一页数码号
echo "共有 $num 条留言";//显示留言数信息
}
else {
echo "<center>当前没有任何留言!</center>";//如果文件内容为空则显示的信息
}
else {
echo "<center>数据文件丢失,请联系管理员!或发布留言重新建立数据文件!</center>";//如果文件不存在显示的信息
}

Copy after login

3. Data modification procedure:

$list=file("gb.dat");//读取整个gb.dat文件到数组$list,数组每一个元素为一条留言($list[0]是第一条留言的数据、$list[1]是第一条留言的数据.....
$n=count($list);//计算$list内容里的留言总数,并赋予变量$n
if ($n>0){ //如果留言数大于0
$fp=fopen("gb.dat","w");//则以只写模式打开文件gb.dat
$gb_reply=encode($gb_reply);
for ($i=0;$i<$n;$i++) {//进入循环
if(eregi($ttime,$list[$i])){//将送来发留言时间$ttime与数组单元$list里内容进行字串匹配比较
$f=explode("|",$list[$i]);//如果找到匹配,就以"|"作为分隔符,切开留言信息$list[$i](第$i条留言),并将这些数据赋予数组$f
$f[9]=$gb_reply;//将$f[9](留言信息最后一条数据)用$gb_reply(回复内容)代替。 
$list[$i]=$f[0]."|".$f[1]."|".$f[2]."|".$f[3]."|".$f[4]."|".$f[5]."|".$f[6]."|".$f[7]."|".$f[8]."|".$f[9]." "; //将数组单元$list[$i]的内容用数组$f加上分隔符"|"代替(其中$f[9]是修改了的新数据)。
break;//跳出循环
}
}//循环结束符
}
FOR($i=0;$i<=$n;$i++){//进入循环
fwrite($fp,$list[$i]);//将数组$list的每个单元为一行,写入文件gb.dat
}//循环结束符 
fclose($fp);//关闭文件
showmessage("回复成功!","index.php");//回复成功,自动返回主界面。

Copy after login

4. Data deletion procedure:

$list=file("gb.dat");//读取整个gb.dat文件到数组$list,数组每一个元素为一条留言($list[0]是第一条留言的数据、$list[1]是第一条留言的数据.....
$n=count($list);//计算$list内容里的留言总数,并赋予变量$n
if ($n>0){//如果留言数大于0
$fp=fopen("gb.dat","w");//则以只写模式打开文件gb.dat
for ($i=0;$i<$n;$i++) {//进入循环
if(eregi($ttime,$list[$i])){//将发送过来发留言时间$ttime与数组$list[$i]里的字串进行匹配比较 
$list[$i]="";//如果匹配成功,则将$list[$i]清空(达到删除的目的)
break;//跳出循环
}
}//循环结束符 
FOR($i=0;$i<=$n;$i++){//进入循环
fwrite($fp,$list[$i]);//将数组$list的每个单元为一行,写入文件gb.dat
} //循环结束符
fclose($fp);//关闭文件
showmessage("删除成功!","index.php");//删除成功,自动返回主界面。
}

Copy after login

5. Data query program:

<form action="search.php" method="post">
<font color="#0099CC" size="2">搜索关键字: 
<input name="found" type="text" id="found" style="background-color:#FFFFFF; color:#8888AA; border: 1 double #3399CC" size="12">
<input name="submit" type="image" src="image/search.gif" alt="留言搜索">
</font></td>
</tr>
</table>
</form>
////////////////////////////////上面是搜索表单语句段
<&#63;
$id=0;
$list=file("gb.dat");//读取整个gb.dat文件到数组$list,数组每一个元素为一条留言($list[0]是第一条留言的数据、$list[1]是第一条留言的数据.....
$n=count($list);//计算$list内容里的留言总数,并赋予变量$n
$found=trim($found);
if (!$found){ //如果$found为假
echo "<center>您没有输入任何关键字!</center>";//作相关显示
}
else {
if($n>0){//如果留言数大于0
for ($i=0;$i<$n;$i++) {//进入循环
if(eregi($found,$list[$i])){//输入的关键字与数组$list[$i]里的字串进行匹配比较
$row=explode("|",$list[$i]); $id=1; //如果找到匹配,就以"|"作为分隔符,切开留言信息$list[$i](第$i条留言),并将这些数据赋予数组$row.并将变量$id赋予1,以便作为是否找到匹配的判断。
list($ip,$datetime,$name,$email,$home,$face,$qq,$head,$text,$reply)=$row;//将数组$row里的单元数据按顺序赋予括号里的变量
&#63;>
<img src=<&#63; echo $head &#63;> >//显示客户形象图片
<br>
<font color="#0099CC">昵称【<&#63; echo $name &#63;><font size="2">】<br>//显示客户名
发表于:<&#63; echo $datetime &#63;>//显示留言发表时间
<br> 
<img src=<&#63; echo $face &#63;>>//显示客户留言表情图片
<&#63; echo $name &#63;>说:<&#63; echo $text; &#63;>//显示客户
Copy after login

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/824625.htmlTechArticlePHP can realize five basic operations such as display, addition, modification, deletion and query of text database data. Let's take a guestbook program as an example to briefly describe the PHP implementation of text data...
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

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)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

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)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

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

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

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

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

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

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

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

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

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,

See all articles