我正在尝试创建一个允许用户删除他们留下的帖子的功能。基本上我正在尝试选择一个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'");
}
答案 0 :(得分: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']){...