Delete a User in an SQL Database


User Email: geez@caz.com

User First Name:

User Last Name:


User Email: biff@caz.com

User First Name:

User Last Name:



The code used for index.php is below:


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=
    , initial-scale=1.0">
    <link rel="stylesheet" href="../styles.css">
    <title>Update Database Contents</title>
</head>
<body>

<h2 style="background-color: yellow;">Delete a User in an SQL Database</h2>

<?php 
    include('../connection.php');
    
    $limit = 5;
    $sql = "SELECT * FROM `users` ORDER BY id DESC LIMIT $limit";
    $result = mysqli_query($link, $sql); 

      foreach ($result as $user){

        $id = $user['id'];
        $email = $user['email'];
        $first_name = $user['first_name'];
        $last_name = $user['last_name'];

        echo "<hr>";
        echo "<p>User Email: <strong>$email</strong></p>";
        echo "<p>User First Name: <strong>$first_name</strong></p>";
        echo "<p>User Last Name: <strong>$last_name</strong></p>";
        echo "<form action='index.php' method='POST'>
                    <input type='submit' value='Delete' name='delete$id'>
        </form>"; 

        if(isset($_POST["delete$id"])){
            // $sql = "SELECT * FROM `users` WHERE 'id' = $id";
            $sql = "DELETE FROM `users` WHERE id = $id";
            mysqli_query($link, $sql); 
            header('location: index.php');
        }
      }    
?>

<hr>

<?php
    include('../show_code.php');
    show_code('index.php');

?>
    
</body>
</html>