Deleting record with image from MySQL table

How to display images from MySQL database records
While displaying records we will add one Delete link to each record. This delete link will pass the value of product id (p_id ) to our JQuery script.
While displaying records, to each row we will assign one unique id by using the value of p_id. The Delete Image at right side carries the id value of p_id.
while ($row = $stmt->fetch_assoc()) {
echo "<div class='row align-middle' id=d_$row[p_id] >
<div class='col-md-3'>
<img src=images/$row[img] class='rounded-circle' alt='$row[p_name]'>
<div class='col-md-2'>$row[p_name]</div>
<div class='col-md-1'>$row[p_id]</div>
<div class='col-md-1'>$row[price]</div>
<div class='col-md-1'><span id=$row[p_id] class=del>Del</span></div>
As we define Delete button within one span tag with class=del , so by clicking any of the Delete buttons the JQuery code below will be triggered.
var id=$(this).attr('id');
if(return_data.records_affected == 1){
setTimeout(function() { $("#msg_display").fadeOut('slow'); }, 4000);
Within the JQuery code by using http GET method we will send the p_id value to our backend PHP script delete-record.php ( for MySQLi connection type ) .
If the record deletion process is successful at backend script then hide the row ( record ) with same id.
if(return_data.records_affected == 1){
This backend script delete-record.php collects the p_id value and after processing the delete query , post back the outcome of the script to front end JQuery function. The same message is displayed to the user by using id=msg_display div tag.
setTimeout(function() { $("#msg_display").fadeOut('slow'); }, 4000);

delete-record.php Backend PHP script.

This script first receives the posted data i.e the value of p_id .
After getting the value of p_id , it will collect the image name from the matching record .
if($stmt = $connection->prepare("SELECT img FROM plus2_db_images 
 WHERE p_id=?")){
   $result = $stmt->get_result();
   //echo "No of records : ".$result->num_rows."<br>";
Now let us delete the record matching with p_id value.
$query="DELETE FROM plus2_db_images WHERE p_id=?";
$stmt = $connection->prepare($query);
if ($stmt) {
$stmt->bind_param('i', $p_id);
$elements['msg'].=" Record Deleted <br>";
If the record is deleted then delete the image from directory using unlink()
if(@unlink("images/$file_name")) {
$elements['msg'].=" File Deleted ";
$elements['msg'].=" File Not Deleted ";
Post back the success or failure message to front end JQuery.
echo json_encode($elements);

Downloading Zip file

Download the zip file and use this instructions to run the file.
How to Install and test
  • Download the zip file at the end of this page.
  • include\config.php : Add your database login details ( MySQLi connection ).
  • include\config-pdo.php : Add your database login details ( PDO connection ).
  • index.php : PHP script to show images from database using MySQLi.
  • index-pdo.php : PHP script to show images from database using PDO.
  • add-record.php : Add image with details to database.
  • delete-record.php : Delete image & database record.
  • sql_dump.txt : MySQL dump to create database table.
(only Display Image )

( With Add and Delete Image )

Subscribe to our YouTube Channel here


* indicates required
Subscribe to plus2net

    Post your comments , suggestion , error , requirements etc here .

    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 All rights reserved worldwide Privacy Policy Disclaimer