How can the issue of not being able to delete the same image multiple times from the database be resolved in PHP?

Issue: The problem of not being able to delete the same image multiple times from the database can be resolved by checking if the image exists in the database before attempting to delete it. This can be done by querying the database to see if the image is present, and only deleting it if it is found. PHP code snippet:

<?php

// Connect to the database
$connection = new mysqli("localhost", "username", "password", "database_name");

// Check if the image exists in the database
$image_id = $_POST['image_id'];
$query = "SELECT * FROM images WHERE id = $image_id";
$result = $connection->query($query);

if($result->num_rows > 0) {
    // Image exists, proceed with deletion
    $delete_query = "DELETE FROM images WHERE id = $image_id";
    $connection->query($delete_query);
    echo "Image deleted successfully.";
} else {
    // Image does not exist
    echo "Image not found in the database.";
}

// Close the database connection
$connection->close();

?>