Php add, delete, modify, and query demo written by myself

Source: Internet
Author: User
Addition, deletion, modification, and query are the most basic operations on the database. The following describes how to connect to the database and how to add, delete, modify, and query data. if you are interested, refer to section 1. common method for connecting to a database: conn. php
The code is as follows:
// Step 1: connect to the database
$ Conn = @ mysql_connect ("localhost: 3306", "root", "root") or die ("mysql link failed ");
// Step 2: Select the specified database and set the character set
@ Mysql_select_db ("php_blog", $ conn) or die ("db link failed". mysql_error ());
Mysql_query ('set NAMES utf8') or die ("character SET setting error ");
?>

2. add. php
The code is as follows:
Include ("conn. php"); // introduce the linked database
If (! Empty ($ _ POST ['sub']) {
$ Title = $ _ POST ['title'];
$ Con = $ _ POST ['con '];
Echo $ SQL = "insert into news (id, title, dates, contents) value (null, '$ title', now (),' $ con ')";
Mysql_query ($ SQL );
Echo "successful insertion ";
}
?>


3. delete del. php
The code is as follows:
Include ("conn. php"); // introduce the linked database
 Include ("conn. php"); // introduce the linked database
If (! Empty ($ _ GET ['id']) {
$ SQL = "select * from news where id = '". $ _ GET ['id']. "'";
$ Query = mysql_query ($ SQL );
$ Rs = mysql_fetch_array ($ query );
}
If (! Empty ($ _ POST ['sub']) {
$ Title = $ _ POST ['title'];
$ Con = $ _ POST ['con '];
$ Hid = $ _ POST ['hid '];
$ SQL = "update news set title = '$ title', contents =' $ con 'where id =' $ hid 'limit 1 ";
Mysql_query ($ SQL );
Echo "script" alert ('updated successfully'); location. href = 'index. php' script ";
Echo "update successful ";
}
?>


If (! Empty ($ _ GET ['Del ']) {$ d = $ _ GET ['Del']; $ SQL = "delete from news where id = '$ d'" ;}$ query = mysql_query ($ SQL); echo "deleted successfully" ;?>


 



4. modify the edit. php page.





 Include ("conn. php"); // introduce the linked database
If (! Empty ($ _ GET ['id']) {
$ SQL = "select * from news where id = '". $ _ GET ['id']. "'";
$ Query = mysql_query ($ SQL );
$ Rs = mysql_fetch_array ($ query );
}
If (! Empty ($ _ POST ['sub']) {
$ Title = $ _ POST ['title'];
$ Con = $ _ POST ['con '];
$ Hid = $ _ POST ['hid '];
$ SQL = "update news set title = '$ title', contents =' $ con 'where id =' $ hid 'limit 1 ";
Mysql_query ($ SQL );
Echo "script" alert ('updated successfully'); location. href = 'index. php' script ";
Echo "update successful ";
}
?>


5. Check, list page
Add content



Include ("conn. php"); // introduce the linked database
If (! Empty ($ _ GET ['Keys ']) {
$ W = "title like '%". $ _ GET ['Keys']. "% '";
} Else {
$ W = 1;
}
$ SQL = "select * from news where $ w order by id desc ";
$ Query = mysql_query ($ SQL );
While ($ rs = mysql_fetch_array ($ query )){
?>
Title: "> "> Edit |"> Delete



  • }
    ?>





    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.