防止删除 ID 1


Prevent ID 1 from being deleted

我有一个代码,可以获取帐户的ID并将其删除在数据库中但我需要防止 ID 1 被删除。

我该怎么做?

   <?php
    // connect to the database
    include('dbconn.php');
    // confirm that the 'id' variable has been set
    if (isset($_GET['admin_ID']) && is_numeric($_GET['admin_ID']))
    {
    // get the 'id' variable from the URL
    $id = $_GET['admin_ID'];
    // delete record from database
    if ($stmt = $con->prepare("DELETE FROM tbl_admin WHERE admin_ID = ? LIMIT 1"))
    {
    $stmt->bind_param("i",$id);
    $stmt->execute();
    $stmt->close();
    }
    else
    {
    echo "ERROR: could not prepare SQL statement.";
    }
    $con->close();
    // redirect user after delete is successful
    header("Location: DeleteAdmin.php");
    }
    else
    // if the 'id' variable isn't set, redirect the user
    {
    header("Location: DeleteAdmin.php");
    }
    ?>

DELETE FROM tbl_admin WHERE admin_ID = ? LIMIT 1更改为DELETE FROM tbl_admin WHERE admin_ID = ? AND admin_ID != 1假设您是主 id,则不需要LIMIT 1,因此永远不会有两个具有相同 id 的 id。