PHPで開発したニュース管理シ...ログイン

PHPで開発したニュース管理システムの削除機能の実装

表示ページについては、変更と削除の両方に ID を出力するステートメントがあります:

<a href="modifynew.php?id=<?php echo $row[' id'] ;?>">変更</a>

<a href="delnew.php?id=<?php echo $row['id'];?>">削除< / a>

ご覧のとおり、削除をクリックして delnew.php ページにジャンプします

注: 削除するには、ID を取得し、データベースにクエリを実行して削除ステートメントを記述する必要もあります。条件が必要です。そうしないと、どのデータを削除するかがわかりません。削除関数のフローチャート:

まずデータベースに接続しますdel.png

header("Content-type: text/html; charset=utf-8") ;//エンコーディングを設定します

$con =@ mysql_connect("localhost","root","root") または die("データベース接続に失敗しました");

mysql_select_db('news') または die("指定されたデータベースを使用できませんopens");

mysql_query("set names utf8 ");//データベースの文字セットを設定します

そして ID を取得します

$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" & lt; スクリプト & gt; local .href = 'newList.php'; & lt;/script & gt; ";


完全なコードは次のとおりです

<?php
    header("Content-type: text/html; charset=utf-8");//设置编码
    $con =@mysql_connect("localhost","root","root") or die("数据库连接失败");
    mysql_select_db('news') or die("指定的数据库不能打开");
    mysql_query("set names utf8");//设置数据库的字符集

    $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>";
    }
?>
次のセクション
<?php header("Content-type: text/html; charset=utf-8");//设置编码 $con =@mysql_connect("localhost","root","root") or die("数据库连接失败"); mysql_select_db('news') or die("指定的数据库不能打开"); mysql_query("set names utf8");//设置数据库的字符集 $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>"; } ?>
コースウェア