In the process of learning PHP believe that a lot of people, all their own to try to develop a variety of functions, that have PHP to write a blog it? This article we will step by step to teach you how to use PHP to achieve a blog, hope that through learning you can write a PHP blog.
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 the 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>
<form action= "add.php" method= "post" > title :<br> <input type= "text" name= "title" > <br><br> contents:<br> <textarea rows= "5" cols= "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, the include (or require) statement gets all the text/code/tags that exist in the specified file, and is copied to the file using the include statement.
Then, when the content of the Name= ' Sub ' in the form is not empty, the contents of the form are obtained, and then the $SQL statement is executed, and NULL indicates that the ID is empty (self-increment), and now () represents the current date, $title and $con to 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; if all the blog content is empty, and the title, date, and body of each article are displayed cyclically. 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.
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 of the ID corresponding to the title, date and body query and display.
And outside a small function is to show a simple counter, each refresh the page, the number of clicks plus 1.
Edit Blog
Create the edit.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>
<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=" name= "Con" >< ? php echo $rs [' contents '];? ></textarea><br><br> <input type= "Submit" Name= "sub" value= "Submit" > < /form>
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>
<?php include ("conn.php");//import Connection Database if (!empty ($_get[' id '))) { $del = $_get[' id ']; Delete blog $sql = "Delete from blog where id= ' $del '"; mysql_query ($sql); echo "Delete success!"; }? >
Finally, the realization of the deletion of the blog function, through the ID of the blog query out and display.
Well, a blog is done, although the interface is not very beautiful, but its function is and perfect, interested in the small partners hurriedly hands-on practice.
Related recommendations:
PHP Blog Website Development Example Tutorial (1/8) _php Tutorial
PHP Blog
PHP Blog Website Development Example Tutorial (1/8)