如何實(shí)現(xiàn)刪除功能
在背景的主頁(yè)中,我們發(fā)現(xiàn)如下圖所示的操作欄位下的 修改/刪除?的按鈕圖案。本章節(jié)我們先來說一說如何實(shí)現(xiàn)刪除一條資料的功能。
刪除功能實(shí)現(xiàn)的主要想法是取得需要?jiǎng)h除的資料的?id,透過SQL語(yǔ)句刪除此書的id來刪除此id在資料庫(kù)表中的全部記錄。
首先建立一個(gè) delete.php 檔案?
#在list.php中進(jìn)行取得id 的路徑再進(jìn)行刪除操作。在?list.php中做如下的修改,$rows["id"]透過上一節(jié)的 while 迴圈輸出。
<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?檔案中匯入資料庫(kù)公用檔案config.php,取得id的值並刪除資料庫(kù)中的此資料?寫入如下的程式碼:
<?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>"; } ?>