PHP additions and deletions to check the example of their own written demo_php instance

Source: Internet
Author: User
1. Linked database Common method: conn.php
Copy Code code as follows:

<?php
First step: Link the database
$conn = @mysql_connect ("localhost:3306", "root", "root") or Die ("MySQL link failed");
Step Two: Select the specified database, set up the character set
@mysql_select_db ("Php_blog", $conn) or Die ("DB link Failed". mysql_error ());
mysql_query (' Set NAMES UTF8 ') or Die ("wrong character set setting");
?>

2. Increase add.php
Copy Code code as follows:

<?php
Include ("conn.php");//introduce a 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 "Insert Success";
}
?>
<form action= "add.php" method= "POST" >
Title: <input type= "text" name= "title" ><br>
Content: <textarea rows= "5" cols= "a" name= "con" ></textarea><br>
<input type= "Submit" Name= "sub" Value= "published" >
</form>

3. Delete del.php
Copy Code code as follows:

<?php
Include ("conn.php");//introduce the linked database <pre name= "code" class= "HTML" ><?php
Include ("conn.php");//introduce a 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 (' Update succeeded '); location.href= ' index.php ' </script> ';
echo "Update succeeded";
}
?>
<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= "name=" con "><?php echo $rs [' contents ']?></textarea><br>
<input type= "Submit" Name= "sub" Value= "published" >
</form></pre><br>
if (!empty ($_get[' del ')) {$d =$_get[' del ']; $sql = "Delete from news where id = ' $d '";} $query =mysql_query ($sql); echo "Delete succeeded";?><p></p>
<pre></pre>
<br>
4, Change edit.php page
<p></p>
<p><br>
</p>
<p></p><pre name= "code" class= "HTML" ><?php
Include ("conn.php");//introduce a 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 (' Update succeeded '); location.href= ' index.php ' </script> ';
echo "Update succeeded";
}
?>
<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= "name=" con "><?php echo $rs [' contents ']?></textarea><br>
<input type= "Submit" Name= "sub" Value= "published" >
</form></pre><br>
5. Check, List page <pre name= "code" class= "HTML" ><a href= "add.php" > Add content </a>


<form>
<input type= "text" name= "Keys"/>
<input type= "Submit" Name= "Subs" value= "Search"/>
</form>
<?php
Include ("conn.php");//introduce a 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)) {
?>
<li><?php echo $rs [' dates ']?></li>
<p><?php echo $rs [' contents ']?></p>

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