防止ID 1被删除

时间:2016-04-06 03:27:09

标签: php mysqli

我有一个代码可以获取帐户的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");
    }

    ?>

1 个答案:

答案 0 :(得分:1)

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