The creation of PHP's Micro Blog

Source: Internet
Author: User
Tags apache php form post php and mysql

One, the development of micro-blog ideas

Micro Blog creation, sure we will use the PHP and MySQL between the addition of the deletion check, first look at the mind map:

Build the computer's Apache PHP and MySQL linkage function, open phpMyAdmin, create a database (Phplearn), in this database to create a data table (news).

Second, the development of the various projects required

1, public template (conn.php )

<?php

@mysql_connect ("localhost", "root", "") or Die ("MySQL connection failed");

@mysql_select_db ("Phplearn") or Die ("DB connection Failed");

@mysql_set_charset ("GdK");

mysql_query ("Set names ' GBK '");

?>

The above uses the MySQL and the specific database connection, respectively uses the mysql_connect and the mysql_select_db these two functions, respectively uses to connect the MySQL and the database Phplearn.

Mysql_set_charset is used to specify database encoding, and mysql_query is a database SQL statement execution function that can write SQL statements directly within parentheses.

It is worth noting that the "@" symbol, which is used to screen MySQL error when the hint, to avoid user experience unfriendly and security considerations.

Die (), which is used to give an error when the database connection fails.

2, add post page add.php

<?php

Include (conn.php);

if (!empty ($_post[' Sub ')) {

$title =$_post[' title ';

$con =$_post[' con '];

$sql = "INSERT INTO ' news ' (' IDs ', ' title ', ' dates ', ' contents ') VALUES (null, ' $title ', now (), ' $con ');

mysql_query ($sql);

echo "<script>alert (' Add success '); location.href= ' index.php ';</script>"

}

?>

<div class= "Con" >

<form action= "add.php" method= "POST" >

Title <input type "text" name= "title" ><br/>

Content <textarea rows= ' 5 ' cols= ' name= ' con ' ></textarea><br/>

<input type= ' Submit ' name= ' Sub ' value= ' Post ' >

</form>

</div>

Include (conn.php) invokes the specified file;

Empty () Determines whether the value is null;

$_post gets the value of the form post submission method;

Insert INTO ' table name ' (' Field 1 ', ' Field 2 ', ' Field 3 ', ' Field 4 ' .... VALUES (' value 1 ', ' Value 2 ', ' Value 3 ', ' Value 4 ' .... ),

SQL INSERT statement;

Location.href= "", JS page jump.

3, Home index.php

<div class= "NAV" >

<button><a href= "add.php" > Post </a></button>

<from action= "" method= "get" >

<input type= "text" name= "Keys"/>

<input type= "Submit" Name= "Subs" value= "Search"/>

</form>

</div>

<?php

Include ("conn.php");

if (!empty ($_get[keys])) {

$w = ' title ' Like '% '. _get[keys]. " %‘";

} else[$w = 1;}

$sql = "SELECT * from ' News ' where $w the ORDER by id DESC limit 10";

$query =mysql_query ($sql);

while (Mysql_fetch_array ($querry)) {

?>

<div class= "Main" >

<a href= "view.php?id= <?php $rs [' id ']?>" ><?php echo $rs [' title ']?></a>

<p><?php echo $rs [' contents ']?></p>

<span><?php echo $rs [' dates ']?></span>

<p class= "Oper" >

<a href= "edit.php?id= <?php $rs [' id ']?> ' > Edit </a>

<a href= "del.php?id= <?php $rs [' id ']?>" > Delete </a>

</p>

</div>

<?php

}

?>

SELECT * from ' table name ' [where] [order] [limit], SQL query statement.

$_get form Get Submit method, different from post, is used for query, running efficiency is high, but the security is poor.

Mysql_fetch_array () to convert the database resource type to an array.

4, delete the post page del.php

<?php

Include ("conn.php");

if (!empty ($_get[' del ')) {

$d =$_get[' del '),;

$sql = "Delete from ' news ' where ' id ' = ' $d '";

mysql_query ($sql);

echo "<scripr>alert (' deletion succeeded '); localtion.herf= ' index.php ';</script> ";

}

?>

Delete from ' table name ' [where] ..., remove the SQL statement.

}

?>

5, modify the Post page edit.php

<?php

Include ("conn.php");

if (!empty ($_get[' id ')) {

$id =$_get[' id '];

$sql = "SELECT * from ' News ' where ' id ' =[' $id ']";

$query =mysql_query (' $sql ');

$rs =mysql_fetch_array ($query);

}

if (!empty ($_post[' hid ')) {

$title =$_post[' title ';

$con =$_post[' contents '];

$hid =$_post[' hid ';

$sql = ' Update ' news ' set ' title ' = ' $title ' contents ' = ' $con ' where ' id ' = ' $hid ' limit 1 '

echo "<script> alert (' Update successful '); location.href= ' index.php ';</script> "

}

? >

<div class= "Con" >

<form action= "edit.php" method= "POST" >

<input type= "Hiden" name= "hid" value= "<?php echo $rs [' id ']?>" >

Title <input type= "text" name= "title" Value= "<?php echo $rs [' title ']?>" >

Content <textarea rows= "5" cols= "name=" con "><?php echo $rs [' contents ']?></textarea><br/>

<input type= "Submit" Name= "sub" value= "POST" >

</form>

</div>

Updates the data for the specified ID and needs to obtain the corresponding specified ID, so you need to set the specified ID for tuning.

6, Post page content view.php

<div class= ' nav ' >

<button><a href= "index.php" > Back to Home </a></button>

</div>

<?php

Include ("conn.php");

  

if (!empty ($_get[' id ')) {

$sql = "SELECT * from ' News ' where ' id ' = '". $_get[' id ' "" ";

$query =mysql_query ($sql);

$rs =mysql_fetch_array ($query);

$sqlup = "Update ' news ' Set hits=hits+1 where ' id ' = '". _get[' id '). "'";

mysql_query ($sqlup);

}  

?>

<div class= "Main" >

<span><?php echo $rs [' Date ']?></span>

<span> click volume; <?php echo $rs [' Hits ']></span>

</p>

</div> "

  

The creation of PHP's Micro Blog

Related Article

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.