How to make a simple blog in PHP

Source: Internet
Author: User
Tags directory create
Recently, have time to read a bit of PHP the code.

The first is the integrated environment

First,phpMyAdmin creates a blog table.

Pure interface operation, the process is relatively simple, it is important to note that the ID is the primary key, and set the auto_increnent option, indicating that the field is empty when self-increment. Other fields are more casual, note the type and length.

Create a data connection

Create the conn.php file under 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 '");? >

MySQL Default user name is root, password is empty, the blog created here in the test Library, so need to connect Test Library.

Add blog

Create the add.php file under 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 upper part is php code, include (or require / code / mark, and copy to use include statement in the file.

Then, when you determine that the contents of the name=' Sub ' in the form are not empty, the contents of the form are obtained, and then the $sql statementis executed, and thenull represents the ID is empty (self-increment),now() represents the current date,$title and $con Fetch the content submitted by the user in the form. Finally Eche Insert a successful prompt.

The lower half is a simple HTML code that enables a blog form to be submitted.

Create The first page of a blog

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

<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>

This page contains some features or more.

The first is a search form, through if determine whether the content of the search form is empty, if not empty, by entering keywords to match the title of the article and display the results; and cycle through the title, date, and body of each post. Clicking on the title will link to the detailed page of the blog. Each article provides "edit" and "delete" functionality.

mysql_query () is used to execute SQL statements. Mysql_fetch_arry () generates an array of the returned data so that it can manipulate every piece of data in the database like an array of operations.

The text is then displayed, with the ICONV_SUBSTR () function to extract the first Five characters of the body.

View Blog

Create the view.php file under the ./wamp/www/blog/ directory .

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

the body of the blog is simple to implement, get the ID of the blog through a get request , and then through the SQL statement adds the ID the corresponding title, date, and body are queried and displayed.

And outside a small function is to show a simple counter, each refresh the page, the number of clicks plus 1.

Edit Blog

In ./wamp/www/blog/ directory Create Span style= "FONT-FAMILY:CALIBRI;" >edit.php file.

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

The ability to edit blogs is relatively complex. Divided into two operations, the first step of the blog title and text query out, and display to the input box. The second step is to update the edited content to the database.

Delete blog

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

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

Finally, the realization of the deletion of the blog function, through the ID of the blog query out and display.

Because all the pages do not use the front-end style has landscaping, very ugly does not map. The function is perfect. In this record, do PHP Learning collation.

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

In addition, although each language has advantages and disadvantages, here still can not help to spit out The two of PHP bad.

1, the symbol is not good to write, "$", "," and "=". These symbols do not add to the difficulty of understanding code syntax. But it's disgusting to knock it up. Every time you hit "$" sign, you have to see the keyboard press shift key to find 4 where.

2, PHP and html mixed in my opinion is not too elegant.

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.