-1

我正在尝试创建一个允许用户删除他们留下的帖子的功能。基本上我正在尝试选择一个 ID,然后将其传递给另一个函数以将其删除。当前的代码正在删除每条评论,而不仅仅是像我尝试的那样链接到 ID 的评论。

这是表格值

Table eventUpdates
ID - unique ID. Trying to use this value to delete the post
eventID - references an eventID from another table
eventReply - the "message" or response
eventUserID -ID of the user posting the message
username -username of the person
eventTimestamp -timestamp

因此,目前每个回复都被删除。我不能让它只删除我发送的 ID。

<table border ="1">
<?
$eventUpdates = mysql_query($sql);
while ($list = mysql_fetch_assoc($eventUpdates)) {  
        echo $updateID;
                echo '<tr><td>';
                    echo $list['username'],"</br>", $list['eventTimestamp'],'<br/>';
                        if($list['eventUserID'] == $userid){ //used for deleting posts. Checks the session to make sure it's the user who made the post
                                ?>
                                <form method="post">
                                <input type="submit" name="deleteUpdate" value="Delete Update">
                                </form>
                                <?
                                if(isset($_POST['deleteUpdate'])){
                                    $updateID = $list['ID'];
                                    $delete = new postprocessing;
                                    $delete->deleteEventUpdate($updateID);
                                    echo '<meta http-equiv="refresh" content="0;url=main.php">';
                                }
                        }
                        echo '</td><td>';
                        echo $list['eventReply'];
                        echo '</td></tr>';
}

这是用于删除它的函数

function deleteEventUpdate($ID){
   mysql_query("delete from eventUpdates where ID = '$ID'");
}
4

1 回答 1

1

这是因为您没有在帖子数组中指定要删除的 ID,因此他们创建的每条评论都“匹配”删除条件。$_POST数组看起来像:

$_POST = array('deleteUpdate'='删除更新');

因此,当您循环浏览事件列表时,用户“拥有”($list['eventUserID'] == $userid)的每条评论都符合条件“ isset($_POST['deleteUpdate'])”:

您需要包含一个隐藏的输入,其 ID 是您要删除的特定评论并与之匹配:

<form method="post">
<input type="submit" name="deleteUpdate" value="Delete Update">
<input name='post_id' value="<?php echo $list['ID'];?>" type='hidden' />
</form>

然后在循环中:

if(isset($_POST['deleteUpdate']) && isset($_POST['post_id']) && $_POST['post_id']==$list['ID']){...
于 2012-04-25T02:15:13.810 回答