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