I have a form that outputs images and their related titles and tags via a while
loop, and at the bottom of this form I have the option to delete an image.
Because the input elements inside the form produce an array of values, when deleting an image I need to run the PDO statements and PHP glob
methods that delete the images from their folders inside a parent foreach loop.
I cannot get this to work though. Without the foreach loop I'm getting the PHP Array to string conversion
error, which I understand/expect. This error throws on the line $stmt->bindParam(':image_id', $image_id);
in the second section of code below.
I think I need to wrap the code from in-between the // ---- START FOREACH ?
and // ---- END FOREACH ?
comments in an associative foreach loop, but I can't work how to do this in terms of the key/value pairs?
Any help would be really appreciated.
Output onto page (the issue is in the block of code after this)
<?php
isset($_REQUEST['username']) ? $username = $_REQUEST['username'] : header("Location: login.php");
$user_id = $_SESSION['logged_in'] ?? header("Location: login.php");
?>
<form method="post" enctype="multipart/form-data">
<?php
$stmt = $connection->prepare("SELECT * FROM lj_imageposts WHERE user_id = :user_id");
$stmt->execute([
':user_id' => $user_id
]);
while ($row = $stmt->fetch()) {
$db_image_id = htmlspecialchars($row['image_id']);
$db_image_filename = htmlspecialchars($row['filename']);
$db_image_ext = htmlspecialchars($row['file_extension']);
?>
<div class="upload-details-component">
<div>
<img src="project/images-lib/image.jpg">
</div>
<div class="edit-zone">
<div class="form-row">
<label for="upload-details-title">Image Title</label>
<input id="upload-details-title" type="text" name="image-title[]">
</div>
<div class="form-row upload-details-form-row">
<label for="upload-details-tags">Comma Separated Image Tags</label>
<textarea name="image-tags[]"></textarea>
</div>
<div class="form-row">
<input type="hidden" name="username" value="<?php echo $username;?>">
<input type="hidden" name="image-id[]" value="<?php echo $db_image_id;?>">
<button name="upload-details-delete[]">DELETE</button>
</div>
</div>
</div>
<?php } ?>
<div class="form-row upload-details-submit-row">
<button type="submit" name="upload-submit">COMPLETE UPLOAD</button>
</div>
</form>
Deleting An Image
<?php
if(isset($_POST['upload-details-delete'])) {
$loggedInUser = $user_id;
$imagesLibrary = 'images-lib/';
$imagesDownload = 'images-download/';
$image_id = $_POST['image-id'];
try {
$sql = "DELETE FROM `lj_imageposts` WHERE image_id = :image_id AND user_id = :user_id";
$stmt = $connection->prepare($sql);
// ---- START FOREACH ?
$stmt->bindParam(':image_id', $image_id);
$stmt->bindParam(':user_id', $loggedInUser);
$stmt->execute();
// delete image files from 'images-lib' folder
foreach(glob($imagesLibrary . $db_image_filename . '-{500,750,1000,1500}' . '.' . $db_image_ext, GLOB_BRACE) as $i) {
unlink($i);
}
// delete files from 'images-download' folder
foreach(glob($imagesDownload . $db_image_filename . '.' . $db_image_ext) as $i) {
unlink($i);
}
// ---- END FOREACH ?
header("Location: upload-details.php?username={$db_username}");
exit;
} catch (PDOException $e) {
echo "Error: " . $e->getMessage();
}
}
?>