Implementation of deletion function of news management system developed with PHP
before row['id'];?>">Modify</a><a href="delnew.php?id=<?php echo $row['id']; ?>">Delete</a>As you can see, click delete and jump to the delnew.php pageNote: To delete, we also need to get the id, and then in When querying the database and writing a delete statement, there must be conditions, otherwise you will not know which data to delete.Flow chart of the delete function:
First of all Connect to the database
header("Content-type: text/html; charset=utf-8");//Set encoding$con =@mysql_connect("localhost","root","root ") or die("Database connection failed");
mysql_select_db('news') or die("The specified database cannot be opened"); mysql_query("set names utf8");//Set the database Character set
Then get the id
$res = mysql_query($sql);
if($res){ echo "<script>alert('Delete successfully');location .href='newlist.php';</script>";
}else{
echo "<script>alert('Deletion failed');location.href='newlist.php';< ;/script>";
}
In this way, our deletion function has been implemented
The complete code is as follows
<?php header("Content-type: text/html; charset=utf-8");//设置编码 $con =@mysql_connect("localhost","root","root") or die("数据库连接失败"); mysql_select_db('news') or die("指定的数据库不能打开"); mysql_query("set names utf8");//设置数据库的字符集 $id = $_GET['id']; $sql = "delete from new where id='$id'"; $res = mysql_query($sql); if($res){ echo "<script>alert('删除成功');location.href='newlist.php';</script>"; }else{ echo "<script>alert('删除失败');location.href='newlist.php';</script>"; } ?>