PHP implements simple blog creation and phpblog

Source: Internet
Author: User

PHP implements simple blog creation and phpblog

Recently, I have read some PHP code. Refer to the PHP100 tutorial for a simple blog, which is a simple record.

The first is the integration environment, here the selection of WAMP: http://www.wampserver.com/en/

First, use phpMyAdmin to create a blog table.

The process is simple. Note that id is the primary key and the auto_increnent option is set to indicate auto-increment when the field is empty. Other fields are more casual. Pay attention to the type and length.

Create a data connection

Create the conn. php file in the./wamp/www/blog directory.

<? Php @ mysql_connect ("127.0.0.1: 3306", "root", "") or die ("mysql database connection failed"); @ mysql_select_db ("test ") or die ("db connection failed"); mysql_query ("set names 'gbk'");?>

The default username of mysql is root, and the password is blank. The blog created here is in the test Library, so you need to connect to the test Library.

Add blog

Create the add. php file in the./wamp/www/blog/directory.

<A href = "index. php "> <B> index </B> </a> <a href =" add. php "> <B> add blog </B> </a> 

This code is divided into two parts: the above part is the PHP code, the include (or require) Statement will get all the text/code/tags in the specified file, and copied to the file using the include statement.

Then, when the content of name = 'sub 'in the form is determined not to be empty, the content of the form is obtained and then the $ SQL statement is executed, null indicates that the id is null (auto-increment), now () indicates that the content submitted by the user in the form is retrieved from the current day, $ title and $ con. The prompt that the eche is successfully inserted.

The lower part is a simple HTML code used to implement a blog form submission function.

Create a blog homepage

Create the index. php file in the./wamp/www/blog/directory.

<A href = "index. php "> <B> index </B> </a> <a href =" add. php "> <B> add blog </B> </a> <br> <form action =" "method =" get "style = 'align: "right" '> <input type = "text" name = "keys"> <input type = "submit" name = "subs"> </form> 

This page contains many features.

First, it is a search form. if is used to determine whether the content of the search form is null. if not empty, enter a keyword to match the title of the article and display the result; if it is null, all the blog content is queried, and the title, date, and body of each article are displayed cyclically. Click the title to link to the detailed page of the blog. Each article provides the "edit" and "delete" functions.

Mysql_query () is used to execute SQL statements. Mysql_fetch_arry () generates an array of returned data so that you can operate each row of data in the database like an array.

The text is displayed. The first 30 characters of the text are extracted using the iconv_substr () function.

View blog

Create a view. php file in the./wamp/www/blog/directory.

<A href = "index. php "> <B> index </B> </a> <a href =" add. php "> <B> add blog </B> </a> 

The implementation of the blog body is relatively simple. get the blog id through the get request, and then query and display the title, date, and body of the id through the SQL statement.
In addition, a small function shows a simple counter. Each time you refresh the page, the number of clicks increases by 1.

Edit blog

Create the edit. php file in the./wamp/www/blog/directory.

<A href = "index. php "> <B> index </B> </a> <a href =" add. php "> <B> add blog </B> </a> 

The function of editing a blog is more complex. There are two operations. The first step is to query the title and body of the blog and display it in the input box. Step 2: update the edited content to the database.

Delete blog

Create the del. php file in the./wamp/www/blog/directory.

<A href = "index. php "> <B> index </B> </a> <a href =" add. php "> <B> add blog </B> </a> 

The last step is to delete a blog and query and display the blog by id.

Because the front-end styles are not used to beautify all pages, they are ugly. The function is still perfect. This record is used to sort out PHP learning.

========================================================== ====================

In addition, although each language has advantages and disadvantages, we can't help but talk about the two disadvantages of PHP.

1. It is hard to write symbols, such as "$", "->", and "=> ". Although these symbols do not increase the difficulty of understanding the code syntax. But it sounds disgusting. Every time you press the "$" symbol, you must watch the keyboard press the shift key to find where 4 is.

2. php and html are not very elegant in my opinion.

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.