How to make a simple blog in PHP
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'");
?>
, 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>
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>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!

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

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

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,

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

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.

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

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.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.
