PHPで開発したニュース管理システムの削除機(jī)能の実裝
表示ページについては、変更と削除の両方に ID を出力するステートメントがあります:
<a href="modifynew.php?id=<?php echo $row[' id'] ;?>">変更</a>
<a href="delnew.php?id=<?php echo $row['id'];?>">削除< / a>
ご覧のとおり、削除をクリックして delnew.php ページにジャンプします
注: 削除するには、ID を取得し、データベースにクエリを?qū)g行して削除ステートメントを記述する必要もあります。條件が必要です。そうしないと、どのデータを削除するかがわかりません。削除関數(shù)のフローチャート:
まずデータベースに接続します
$con =@ mysql_connect("localhost","root","root") または die("データベース接続に失敗しました");
mysql_select_db('news') または die("指定されたデータベースを使用できませんopens"); mysql_query("set names utf8 ");//データベースの文字セットを設(shè)定します
そして ID を取得します
$res = mysql_query($sql);
if($res){ echo "<script>alert('削除成功') ;location.href='newlist.php';</script> ";}} Else {
echo" & lt; スクリプト & gt; local .href = 'newList.php'; & lt;/script & gt; ";
完全なコードは次のとおりです
<?php header("Content-type: text/html; charset=utf-8");//設(shè)置編碼 $con =@mysql_connect("localhost","root","root") or die("數(shù)據(jù)庫連接失敗"); mysql_select_db('news') or die("指定的數(shù)據(jù)庫不能打開"); mysql_query("set names utf8");//設(shè)置數(shù)據(jù)庫的字符集 $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>"; } ?>