PHP開(kāi)發(fā)新聞管理系統(tǒng)之刪除功能的實(shí)現(xiàn)
之前我們講過(guò)展示頁(yè)面,修改和刪除都帶了一個(gè)輸出id的語(yǔ)句,代碼去下:
<a href="modifynew.php?id=<?php echo $row['id'];?>">修改</a>
<a href="delnew.php?id=<?php echo $row['id'];?>">刪除</a>
可以看出,點(diǎn)擊刪除,跳轉(zhuǎn)到 delnew.php 頁(yè)面
注意:刪除,我們也是要獲取id,然后在數(shù)據(jù)庫(kù)進(jìn)行查詢,寫(xiě)刪除語(yǔ)句,必須要有條件,不然就不知道刪除哪條數(shù)據(jù)了
刪除功能的流程圖:
首先連接數(shù)據(jù)庫(kù)
????header("Content-type: text/html; charset=utf-8");//設(shè)置編碼
?? ?$con =@mysql_connect("localhost","root","root") or die("數(shù)據(jù)庫(kù)連接失敗");
?? ?mysql_select_db('news') or die("指定的數(shù)據(jù)庫(kù)不能打開(kāi)");
?? ?mysql_query("set names utf8");//設(shè)置數(shù)據(jù)庫(kù)的字符集
然后獲取 id
$id = $_GET['id'];
最后我們寫(xiě)刪除語(yǔ)句
????$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>";
?? ?}
這樣我們的刪除功能就已經(jīng)實(shí)現(xiàn)了
完整代碼如下
<?php header("Content-type: text/html; charset=utf-8");//設(shè)置編碼 $con =@mysql_connect("localhost","root","root") or die("數(shù)據(jù)庫(kù)連接失敗"); mysql_select_db('news') or die("指定的數(shù)據(jù)庫(kù)不能打開(kāi)"); mysql_query("set names utf8");//設(shè)置數(shù)據(jù)庫(kù)的字符集 $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>"; } ?>