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

Source: Internet
Author: User
Adding, deleting, modifying, and querying a database is the most basic operation. The following describes how to connect to the database and how to add, delete, modify, and query data. For more information, see

Adding, deleting, modifying, and querying a database is the most basic operation. The following describes how to connect to the database and how to add, delete, modify, and query data. For more information, see

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 whereid ']."' ";
$ 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 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 whereid ']."' ";
$ 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 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.