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