如何實現(xiàn)刪除功能
在后臺的主頁面中,我們發(fā)現(xiàn)如下圖所示的操作欄目下的 修改/刪除?的按鈕圖案。本章節(jié)我們就先來說一說如何實現(xiàn)刪除一條數(shù)據(jù)的功能。
刪除功能實現(xiàn)的主要思路是獲取需要刪除的數(shù)據(jù)的?id,通過SQL語句刪除此書的id來刪除此id在數(shù)據(jù)庫表中的的全部記錄。
首先創(chuàng)建一個 delete.php 文件?
在 list.php中進(jìn)行獲取 id 的路徑再進(jìn)行刪除操作。在?list.php中做如下的修改,$rows["id"]通過上一節(jié)的 while 循環(huán)輸出。
<td> <div class="button-group"> <a class="button border-main" href="#"><span class="icon-edit"></span>修 改</a> <a class="button border-red" href="delete.php?id=<?php echo $rows["id"]?>" onclick="return del(1,1,1)"> <span class="icon-trash-o"></span>刪 除 </a> </div> </td>
delete.php?文件中導(dǎo)入數(shù)據(jù)庫公共文件config.php,獲取 id的值并刪除數(shù)據(jù)庫中的此條數(shù)據(jù)?寫入如下的代碼:
<?php header("content-type:text/html;charset=utf-8"); include("config.php"); $id = isset($_GET['id'])?$_GET['id']:""; $sql = "delete from list where id = '$id'"; //echo $sql; $rel = mysqli_query($link,$sql); if($rel){ echo "<script>alert('刪除成功');window.location.href='list.php'</script>"; }else{ echo "<script>alert('刪除失敗');window.location.href='list.php'</script>"; } ?>