I have a foreach loop to post each 'blog' post separately. I want myself (or possibly another moderator) to be able to delete each post if it is inappropriate. When I go through the foreach loop to post each blog, I want to somehow be able to call the EntryID variable for that specific post and then pass that to my delete post function. However, when I try to delete any other post then the last one. It is deleting the last one only. I know the issue is because I am looping through the variables and it is getting the last post shown, however I am completely lost on how I can get it to react how I want.
I have considered trying to append the number back to the blog post, but even still I don't know how to call that variable versus another. I have tried sending an echo statement to see if that will select the right variable but that isn't working either.
<?php
$queryActive = 'SELECT Title, Body, CreateDate, UserName, EntryID
FROM entries
JOIN Users on entries.UserID = Users.UserID
WHERE entries.Active = 1
ORDER BY CreateDate desc, EntryID desc';
$statement = $db->prepare($queryActive);
$statement->execute();
$entries = $statement->fetchAll();
$statement->closeCursor();
$userid = filter_input(INPUT_COOKIE, 'UserID', FILTER_VALIDATE_INT);
echo "UserID ".$userid;
?>
<?php foreach ($entries as $entry) : ?>
<div id="frmDelete">
<tr>
<td>#<?php echo $entry['EntryID'];?></td><br>
<td><?php echo $entry['Title']; ?> - <?php echo $entry['CreateDate']; ?></td><br />
<td><?php echo $entry['Body']; ?> - <?php echo $entry['UserName']; ?></td>
<br />
<td>
<form>
<input type=submit id="btnReply" value="Reply" />
</form>
</td>
<td>
<form action="Delete_entry.php" method="post" id="Delete_entry">
<input type="hidden" readonly="true" name="EntryID" value="<?php echo $entry['EntryID']; ?>" />
<?php if ($userid == 1) { ?>
<input type="submit" id="btnDelete" value="Delete Post"/>
<?php } ?>
<br /><br />
</form>
</td>
</tr>
<?php endforeach; ?>
Excerpt of the Delete_Entry page (Yes I know I need to sanitize before rolling it out to live website, it is still in testing)
<?php
$userid = filter_input(INPUT_COOKIE, 'UserID', FILTER_VALIDATE_INT);
$entryID = $_POST['tempEntryID'];
echo $entryID;
?>
<?php
// Validate Nulls
if ($entryID == null || $userid != "1") {
include 'Invalid_User.php';
} else {
require_once 'databaseBlog.php';
$query = 'UPDATE entries
SET Active = 0
WHERE entryID = :entryID';
$statement = $db->prepare($query);
$statement->bindValue(':entryID', $entryID);
$statement->execute();
$statement->closeCursor();
}
I am expecting the delete button to get the entryID for the post I am selecting, however the entryID is only getting the last variable that was sent. So if I have 50 posts and try to delete 17, it will delete 50.
EDITED Okay, so the issue was simply due to the table cells not being lined up correctly with the form. I have added the new code and it is functioning correctly.