I have a page that fetches images from a database after an initial upload, that allows the user to add titles and tags to the images.
I have set the while
loop to output the individual images inside one form. Each image has two related input fields. What I would like to do is have it so when the form is submitted all of the information is updated in the database in one go (even if image details from the relevant input fields remain blank/are not filled in).
To achieve this I thought I could have the form’s submit button outside of the while
loop. This isn’t working as foreseen though. It basically updates one image at a time, each time I press submit (it updates the last image in the loop).
How do I get it so it processes all of the information for all of the images inside the while loop in one go, on submit? If I place the submit button inside the loop I get a button for every image, which I don't want.
Note: the <img/>
source path I've just hardcoded below to save adding the different variables that achieve this, thus hopefully keeping the code simpler.
Fetch data from database and output HMTL form
<?php isset($_REQUEST['username']) ? $username = $_REQUEST['username'] : header("Location: login.php"); ?>
<form method="post" enctype="multipart/form-data">
<?php
if (isset($_SESSION['logged_in'])) {
$user_id = $_SESSION['logged_in'];
}
$stmt = $connection->prepare("SELECT * FROM lj_imageposts WHERE user_id = :user_id");
$stmt->execute([
':user_id' => $user_id
]);
while ($row = $stmt->fetch()) {
$db_image_filename = htmlspecialchars($row['filename']);
?>
<div class="upload-details-component">
<div class="form-row">
<img src="/project/images/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">
<label for="upload-details-tags">Comma Separated Image Tags</label>
<textarea id="upload-details-tags" type="text" name="image-tags"></textarea>
</div>
<div class="form-row">
<input type="hidden" name="username" value="<?php echo $username; ?>">
<input type="hidden" name="image-filename" value="<?php echo $db_image_filename; ?>">
</div>
</div>
</div>
<?php } ?>
<button type="submit" name="upload-submit">COMPLETE UPLOAD</button>
</form>
Update Database On Form submission
<?php
if(isset($_POST['upload-submit'])) {
$image_title = $_POST['image-title'];
$image_tags = $_POST['image-tags'];
$form_filename = $_POST['image-filename']; // value attribute from hidden form element
try {
$sql = "UPDATE lj_imageposts SET
image_title = :image_title,
image_tags = :image_tags
WHERE filename = :filename";
$stmt = $connection->prepare($sql);
$stmt->execute([
':image_title' => $image_title,
':image_tags' => $image_tags,
':filename' => $form_filename
]);
// This is the URL to this actual page (basically refreshes the page)
header("Location: upload-details.php?username={$username}");
} catch (PDOException $e) {
echo "Error: " . $e->getMessage();
}
// give values an empty string to avoid an error being thrown before form submission if empty
$image_title = $image_tags = "";
}
?>