How can checkboxes be utilized to select and delete specific images from a database and folder in PHP?

To select and delete specific images from a database and folder in PHP using checkboxes, you can create a form with checkboxes for each image. When the form is submitted, iterate through the checkboxes, retrieve the selected images, delete them from the database, and then remove the corresponding image files from the folder.

<?php
// Check if form is submitted
if(isset($_POST['delete_images'])) {
    // Connect to database
    $conn = new mysqli("localhost", "username", "password", "dbname");

    // Iterate through selected checkboxes
    foreach($_POST['image_ids'] as $image_id) {
        // Delete image from database
        $sql = "DELETE FROM images WHERE id = $image_id";
        $conn->query($sql);

        // Remove image file from folder
        $image_path = "images/" . $image_id . ".jpg";
        if(file_exists($image_path)) {
            unlink($image_path);
        }
    }

    // Close database connection
    $conn->close();
}
?>

<form method="post">
    <?php
    // Display checkboxes for each image
    $result = $conn->query("SELECT * FROM images");
    while($row = $result->fetch_assoc()) {
        echo '<input type="checkbox" name="image_ids[]" value="' . $row['id'] . '">' . $row['name'] . '<br>';
    }
    ?>

    <input type="submit" name="delete_images" value="Delete Selected Images">
</form>