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>