Recently, I had time to look at some PHP code.
The first is the integrated environment
First pass, phpMyAdminCreate a blogsurface.
Pure interface operation, the process is relatively simple. It should be noted that id is the primary key, and Set the auto_increnent option to indicate that the field will automatically increment when it is empty. Other fields are more casual, just pay attention to the type and length.
Create a data connection conn.php
file. <?php
@mysql_connect("127.0.0.1:3306","root","") or die("mysql数据库连接失败");
@mysql_select_db("test")or die("db连接失败");mysql_query("set names 'gbk'");
?>
mysqlThe default user name is
, the password is empty, the ## created here
is in the test library, so it needs to be connected to the test library. Addblog
at./wamp/www/blog/ Create the add.php file in the
directory.<a href="index.php"><B>index</B></a> <a href="add.php"><B>add blog</B></a> <hr> <?php include("conn.php"); //引入连接数据库 if (!empty($_POST['sub'])) { $title = $_POST['title']; //获取title表单内容 $con = $_POST['con']; //获取contents表单内容 $sql= "insert into blog values(null,'0','$title',now(),'$con')"; mysql_query($sql); echo "insert success!"; }?> <form action="add.php" method="post"> title :<br> <input type="text" name="title"><br><br> contents:<br> <textarea rows="5" cols="50" name="con"></textarea><br><br> <input type="submit" name="sub" value="submit"> </form>This code is divided into two parts, the upper part is the PHP code,
include ## The
# (or
require) statement will retrieve all text present in the specified file /Code / tag and copy it to a file using the include statement. Then, if it is judged that the content of name='sub' in the form is not empty, the content of the form will be obtained, and then $sql will be executed. statement, null
means id is empty (incremented), now() means taking the current date, $title and $con take the content submitted by the user in the form. Finallyeche prompts for successful insertion. The lower part is a simple HTML code, used to implement a blog form submission Function.
创建blog的首页
在./wamp/www/blog/目录下创建index.php文件。
<a href="index.php"><B>index</B></a> <a href="add.php"><B>add blog</B></a> <br><br> <form action="" method="get" style='align:"right"'> <input type="text" name="keys" > <input type="submit" name="subs" > </form> <hr> <?php include("conn.php"); //引入连接数据库 if (!empty($_GET['keys'])) { $key = $_GET['keys']; $w = " title like '%$key%'"; }else{ $w=1; } $sql ="select * from blog where $w order by id desc limit 5"; $query = mysql_query($sql); while ($rs = mysql_fetch_array($query)) { ?> <h2>title: <a href="view.php?id=<?php echo $rs['id']; ?>"><?php echo $rs['title']; ?></a> | <a href="edit.php?id=<?php echo $rs['id']; ?>">edit</a> | <a href="del.php?id=<?php echo $rs['id']; ?>">delete</a> | </h2> <li>date: <?php echo $rs['data']; ?></li> <!--截取内容展示长度--> <p>contents:<?php echo iconv_substr($rs['contents'],0,30,"gbk"); ?>...</p> <hr> <?php };?>
该页面包含有的功能还是比较多的。
首先是一个搜索表单,通过if判断搜索表单的内容是否为空,如果不为空,通过输入关键字匹配文章的标题并显示结果;如果为空查询所有blog内容,并循环显示每一篇文章的标题、日期、正文。点击标题会链接到该篇blog的详细页面。每一篇文章提供“编辑”和“删除”功能。
mysql_query()用于执行sql语句。mysql_fetch_arry()将返回的数据生成数组,这样就可以像操作数组一样,操作数据库中的每一条数据了。
然后是正文的显示,通过 iconv_substr() 函数提取正文前30个字符。
查看blog
在./wamp/www/blog/目录下创建view.php文件。
<a href="index.php"><B>index</B></a> <a href="add.php"><B>add blog</B></a> <hr> <?php include("conn.php"); //引入连接数据库 if (!empty($_GET['id'])) { $id = $_GET['id']; $sql ="select * from blog where id='$id' "; $query = mysql_query($sql); $rs = mysql_fetch_array($query); $sqlup = "update blog set hits=hits+1 where id='$id'"; mysql_query($sqlup); }?> <h2 id="title-nbsp-php-nbsp-echo-nbsp-rs-title-nbsp">title: <?php echo $rs['title'];?> </h2> <h3>date: <?php echo $rs['data'];?> click number: <?php echo $rs['hits']; ?></h3> <hr> <p>contents:<?php echo $rs['contents']; ?></p>
blog的正文实现比较简单,通过get请求获取blog的id,然后通过sql语句将该id对应的标题、日期和正文查询出来并显示。
并外一个小功能是显示了一个简单的计数器,每刷新页面,点击数加1。
编辑blog
在./wamp/www/blog/目录下创建edit.php文件。
<a href="index.php"><B>index</B></a> <a href="add.php"><B>add blog</B></a> <hr> <?php include("conn.php"); //引入连接数据库 //获取数据库表数据if (!empty($_GET['id'])) { $edit = $_GET['id']; $sql = "select * from blog where id='$edit'"; $query = mysql_query($sql); $rs = mysql_fetch_array($query); }//更新数据库表数据if (!empty($_POST['sub'])) { $title = $_POST['title']; //获取title表单内容 $con = $_POST['con']; //获取contents表单内容 $hid = $_POST['hid']; $sql= "update blog set title='$title', contents='$con' where id='$hid' "; mysql_query($sql); echo "<script>alert('update success.');location.href='index.php'</script>"; }?> <form action="edit.php" method="post"> <input type="hidden" name="hid" value="<?php echo $rs['id'];?>"> title :<br> <input type="text" name="title" value="<?php echo $rs['title'];?>"> <br><br> contents:<br> <textarea rows="5" cols="50" name="con" ><?php echo $rs['contents'];?></textarea><br><br> <input type="submit" name="sub" value="submit"> </form>
编辑blog的功能相对复杂一些。分两部操作,第一步先将blog的标题和正文查询出来,并显示到输入框。第二步将编辑好的内容再更新到数据库中。
删除blog
在./wamp/www/blog/目录下创建del.php文件。
<a href="index.php"><B>index</B></a> <a href="add.php"><B>add blog</B></a> <hr> <?php include("conn.php"); //引入连接数据库 if (!empty($_GET['id'])) { $del = $_GET['id']; //删除blog $sql= "delete from blog where id='$del' "; mysql_query($sql); echo "delete success!"; }?>
最后是实现blog的删除功能,通过id将该条blog的查询出来并显示。
因为所有页面没有使用前端样式有美化,很丑就不贴图了。功能还算完美。在此记录,算做PHP学习的整理。
=======================================================
另外,虽然每个语言都有优缺点,这里还是忍不住要吐槽一下PHP的两个不好之处。
1、符号不好写, “$” 、“ ->” 、 “=>”。这些符号虽然并没有增加代码语法的理解难度。但敲起来具恶心。每次在打“$”符号的时候,都要眼看键盘按着shift键找4在哪儿。
2、php与html的混编在我看来也不是太优雅。
The above is the detailed content of How to make a simple blog in PHP. For more information, please follow other related articles on the PHP Chinese website!

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

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

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

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

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

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

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

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。


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

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 English version
Recommended: Win version, supports code prompts!

Dreamweaver CS6
Visual web development tools

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