← 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.
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 ) .
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.
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=?")){
$stmt->bind_param('i',$p_id);
$stmt->execute();
$result = $stmt->get_result();
//echo "No of records : ".$result->num_rows."<br>";
$row=$result->fetch_object();
$file_name=$row->img;
}else{
$elements['msg'].=$connection->error;
}
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);
$stmt->execute();
$elements['msg'].=" Record Deleted <br>";
$elements['records_affected']=$stmt->affected_rows;
}else{
$elements['msg'].=$connection->error;
}
If the record is deleted then delete the image from directory using unlink()