How to delete data

Project : in this project we will learn how to delete user's data from database.

First We have to fetch all data and display in table then from user table, we will delete data by clicking on delete button.

Click here to understand flow

index (Add data)

Add data : Add data in database table.
myproject/index.php
index.php (add data)
PHP
How to insert data in database Click here

Fetch all data (List of users)

Fetch All : We have to fetch all the data from data that we have previous lession.
myproject/user-list.php
user-list.php
PHP
How to fetch all data in database Click here

Flow of Process

Click on Delete button (check image below)
PHP
Delete button has an id with query string (check image below)
PHP

Data in table

we recieve del_id by clicking on delete button in delete.php (follow as in image below).

PHP

Database Connection

Database exists: Database-> dbdemo Table-> users
Create connection with database
myproject/dbcon.php
dbcon.php
    
        $dbhost ="localhost";
        $dbuser ="root";
        $dbpswd ="";
        $dbname ="dbdemo";
        $dbcon= mysqli_connect($dbhost,$dbuser,$dbpswd,$dbname);
        if($dbcon){
            //echo "Connected............!!!";
            return $dbcon;
            exit();
        }else{
            die("Connection failed ? ? ?". mysqli_error($dbcon));
        }
        mysqli_close($dbcon);
    

Delete

myproject/delete.php
delete.php
                
                  <?php include("dbcon.php"); //This is database connection file
                  $error=''; $success='';
                  if(isset($_GET['del_id'])){
                      $del_id = $_GET['del_id'];
                      $sql="DELETE FROM users WHERE id='$del_id'";
                      $run=mysqli_query($dbcon,$sql);
                      if($run){
                        $success = "<div class='alert alert-success text-success text-center alert-dismissible'><button type='button' class='close' data-dismiss='alert'>×</button>The user has been deleted Successfully</div>";
                        header('location:user-list.php?1='.$success);
                      }else{
                        $error ="<div class='alert alert-success text-success text-center alert-dismissible'><button type='button' class='close' data-dismiss='alert'>×</button>The The user has not been deleted </div>";
                        header('location:user-list.php?0='.$error);
                      }
                  }else{
                    $error = "No data deleted";
                  }
                  mysqli_close($dbcon);
                
            

common

header.php
    
        <!DOCTYPE html>
        <html>
        <head>
            <meta charset="utf-8">
            <meta name="viewport" content="width=device-width, initial-scale=1,
            shrink-to-fit=no">
            <title>Codetechinfo</title>
            <link rel="stylesheet" href="css/bootstrap.min.css">
        </head>
    
navbar.php
    
        <nav class="navbar navbar-expand-lg navbar-light bg-primary text-white fixed-top">
            <a class="navbar-brand text-white" href="./index.php">Learn PHP  </a>
            <div class="collapse navbar-collapse" id="navbarNav">
                <ul class="navbar-nav">
                    <li class="nav-item active">
                        <a class="nav-link text-white" href="./index.php">Home  </a>
                    </li> 
                </ul>
            </div>
        </nav>
        <br><br>
    
footer.php
    
            <div class="copyright py-4 text-center text-white">
                <div class="container "><small>© Copyright 2025 by Saten Chauhan</small> </div>
            </div>
            </body>
        </html>
    

css folder

You must download and include bootstrap css source files for good interface OR You can add cdn link from bootstrap website.
myproject/css
bootstrap.min.css