php add delete change and check the example of demo written by yourself

  • 2020-08-22 21:56:39
  • OfStack

1. Common method for linking databases: ES0en.php
 
<?php 
// The first 1 Step: Link the database  
$conn=@mysql_connect("localhost:3306","root","root")or die ("mysql A link failure "); 
// The first 2 step :  Select the specified database and set the character set  
@mysql_select_db("php_blog",$conn) or die ("db A link failure ".mysql_error()); 
mysql_query('SET NAMES UTF8')or die (" Character set error "); 
?> 

2. Increase add. php
 
<?php 
include("conn.php");// Introducing 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 the success "; 
} 
?> 
<form action="add.php" method="post"> 
 The title : <input type="text" name="title"><br> 
 content : <textarea rows="5" cols="50" name="con"></textarea><br> 
<input type="submit" name="sub" value=" published "> 
</form> 

3. Delete del. php
 
<?php 
include("conn.php");// Introducing a linked database <pre name="code" class="html"><?php 
include("conn.php");// Introducing 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(' The update is successful '); location.href='index.php'</script>"; 
echo" The update is successful "; 
} 
?> 
<form action="edit.php" method="post"> 
<input type="hidden" name="hid" value="<?php echo $rs['id']?>"/> 
 The title : <input type="text" name="title" value="<?php echo $rs['title']?>"><br> 
 content : <textarea rows="5" cols="50" 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 the success "; ?><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");// Introducing 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(' The update is successful '); location.href='index.php'</script>"; 
echo" The update is successful "; 
} 
?> 
<form action="edit.php" method="post"> 
<input type="hidden" name="hid" value="<?php echo $rs['id']?>"/> 
 The title : <input type="text" name="title" value="<?php echo $rs['title']?>"><br> 
 content : <textarea rows="5" cols="50" name="con"><?php echo $rs['contents']?></textarea><br> 
<input type="submit" name="sub" value=" published "> 
</form></pre><br> 
5. Check out 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");// Introducing 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)){ 
?> 
<h2> The title :<a href="view.php?id=<?php echo $rs['id'] ?>"><?php echo $rs['title'] ?></a> <a href="edit.php?id=<?php echo $rs['id'] ?>"> The editor </a> | | <a href="del.php?del=<?php echo $rs['id'] ?>"> delete </a></h2> 
<li><?php echo $rs['dates'] ?></li> 
<p><?php echo $rs['contents'] ?></p> 
<hr> 
<?php 
} 
?> 
</pre><br> 
<p></p> 
<p><br> 
</p> 

Related articles: