PHP開發簡單新聞發布系統之新聞列表頁刪除功能模組

本節介紹本節介紹怎麼從新聞清單頁刪除新聞,並且刪除資料庫中該新聞的記錄。

我們還是離不開操作資料庫,這裡需要使用delete:刪除資料表中的資料

想法:

透過刪除資料路表new表中的id欄位來刪除一則新聞

1608.png

SQL語句為:

<?php
$sql = "delete from new where id = '$id'";
?>

完整的實作刪除功能的delete.php檔:

<?php
 header("content-type:text/html;charset=utf8");
 $link = mysqli_connect('localhost','root','root','test');
       mysqli_set_charset($link, "utf8");
       
 if (!$link) {
   die("连接失败:".mysqli_connect_error());
 }
 $id = $_GET['id'];
 $sql = "delete from new 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>";
 }
?>


繼續學習
||
<?php header("content-type:text/html;charset=utf8"); $link = mysqli_connect('localhost','root','root','test'); mysqli_set_charset($link, "utf8"); if (!$link) { die("连接失败:".mysqli_connect_error()); } $id = $_GET['id']; $sql = "delete from new 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>"; } ?>
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!