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

Source: Internet
Author: User

1. Common method for connecting to a database: conn. php
Copy codeThe Code is as follows:
<? Php
// 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
Copy codeThe Code is as follows:
<? Php
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 ";
}
?>
<Form action = "add. php" method = "post">
Title: <input type = "text" name = "title"> <br>
Content: <textarea rows = "5" cols = "50" name = "con"> </textarea> <br>
<Input type = "submit" name = "sub" value = "">
</Form>

3. Delete del. php
Copy codeThe Code is as follows:
<? Php
Include ("conn. php"); // introduce the Linked database <pre name = "code" class = "html"> <? Php
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 ";
}
?>
<Form action = "edit. php" method = "post">
<Input type = "hidden" name = "hid" value = "<? Php echo $ rs ['id']?> "/>
Title: <input type = "text" name = "title" value = "<? Php echo $ rs ['title']?> "> <Br>
Content: <textarea rows = "5" cols = "50" name = "con"> <? Php echo $ rs ['tents']?> </Textarea> <br>
<Input type = "submit" name = "sub" value = "">
</Form> </pre> <br>
If (! Empty ($ _ GET ['del ']) {$ d = $ _ GET ['del']; $ SQL = "delete from news where id = '$ d'" ;}$ query = mysql_query ($ SQL); echo "deleted successfully" ;?> <P> </p>
<Pre> </pre>
<Br>
4. Modify the edit. php page.
<P> </p>
<P> <br>
</P>
<P> </p> <pre name = "code" class = "html"> <? Php
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 ";
}
?>
<Form action = "edit. php" method = "post">
<Input type = "hidden" name = "hid" value = "<? Php echo $ rs ['id']?> "/>
Title: <input type = "text" name = "title" value = "<? Php echo $ rs ['title']?> "> <Br>
Content: <textarea rows = "5" cols = "50" name = "con"> <? Php echo $ rs ['tents']?> </Textarea> <br>
<Input type = "submit" name = "sub" value = "">
</Form> </pre> <br>
5. Check. On the list page, <pre name = "code" class = "html"> <a href = "add. php"> add content </a>
<Hr>
<Hr>
<Form>
<Input type = "text" name = "keys"/>
<Input type = "submit" name = "subs" value = "Search"/>
</Form>
<? Php
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 )){
?>
<H2> title: <a href = "view. php? Id = <? Php echo $ rs ['id']?> "> <? Php echo $ rs ['title']?> </A> <a href = "edit. php? Id = <? Php echo $ rs ['id']?> "> Edit </a> | <a href =" del. php? Del = <? Php echo $ rs ['id']?> "> Delete </a> <Li> <? Php echo $ rs ['dates']?> </Li>
<P> <? Php echo $ rs ['tents']?> </P>
<Hr>
<? Php
}
?>
</Pre> <br>
<P> </p>
<P> <br>
</P>

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.