dsw7547 2019-04-28 18:29
浏览 60

foreach循环中的变量仅在Posting to Delete_Entry页面时获取最后一个数字

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.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 msix packaging tool打包问题
    • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
    • ¥15 python的qt5界面
    • ¥15 无线电能传输系统MATLAB仿真问题
    • ¥50 如何用脚本实现输入法的热键设置
    • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
    • ¥30 深度学习,前后端连接
    • ¥15 孟德尔随机化结果不一致
    • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
    • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线