How to delete image from folder & database

Project : In this project we will learn how to delete image from table, folder and database .

Click on Delete button {check image below)
myproject/imagetable.php
imagetable.php
PHP How to add and fetch all images Click here Delete button has an id with query string (check image below) PHP

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);
    

Process to delete image

Process to delete image from upload folder and database.

delete.php
              
                <?php include("dbcon.php");
                $error=''; $success='';
                if(isset($_GET['del'])){
                    $del_img = $_GET['del_img']; // from href="imagetable.php?del=<?php echo $row['id'];&del_img=<?php echo $row['pic_name'];"
                    $del_id = $_GET['del'];
                    $sql="DELETE FROM images WHERE id='$del_id'";
                    $result = mysqli_query($dbcon, $sql);
                    if($result){
                        $success="<div class='alert alert-success'>Image has been deleted </div>";
                        if(@unlink("upload/$del_img")){
                            $del_img=''; $del_id='';
                            //header("refresh:1; url=imagetable.php");
                        }   
                    }else{
                        $error = "<div class='alert alert-danger'>Image has not been deleted ?</div>";
                    }
                } 
                mysqli_close($dbcon);
                
            

delete.php (a bit different)
              
                <?php include("dbcon.php"); 
                $error=''; $success='';
                if(isset($_GET['del'])){
                    $del_id = $_GET['del'];
                    $sql = "SELECT pic_name FROM images WHERE id='$del_id'";
                    $run = mysqli_query($dbcon,$sql);
                    $image = mysqli_fetch_assoc($run);
                    $del_img = $image['pic_name'];
                    $sql="DELETE FROM images WHERE id='$del_id'";
                    $result = mysqli_query($dbcon, $sql);
                    @unlink("upload/$del_img")
                    if($result){
                        $success="<div class='alert alert-success'>Image has been deleted </div>";
                        $del_img=''; $del_id='';
                        //header("refresh:1; url=imagetable.php");
                    }else{
                        $error = "<div class='alert alert-danger'>Image has not been deleted ?</div>";
                    }
                  }
              
            

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