[英]MYSQL delete specific row button PHP
我正在建立一个非常基本的论坛,可以对帖子发表评论。 如您所见,此代码显示注释,当注释作者等于登录用户时,它应显示一个删除按钮以删除注释。 现在的问题是,当我单击“删除”按钮时,它将删除帖子上的所有评论,而不仅是旁边带有删除按钮的评论。
我的“ comments”表具有id,discid,comment_username,comment和comment_time列,其中来自“ comments”的discid列引用了“ disussions”表中的id列
<?php
$id = mysql_real_escape_string($_GET['id']);
$query = 'SELECT * FROM discussions WHERE `id` = '.$id.' LIMIT 1';
$result = mysql_query($query);
$row = mysql_fetch_array($result);
?>
<h2><? echo $row['category'] . $row['topic']; ?></h2>
<div>Posted on <? echo $row['date_time']; ?> by <? echo $row['username'] ?></div>
<div><? echo $row['discussion']; ?></div>
<?
$result_comments = mysql_query("SELECT * FROM comments WHERE discid = $id ORDER BY comment_time ASC");
while ($record = mysql_fetch_array($result_comments))
{
?>
<form action="" method="POST" >
<? echo $record['comment_username']. $record['comment_time'] ?>
<!-- if comment from logged in user, show delete option -->
<?
if ($record['comment_username'] == $log_username)
{
echo '<span><button title="Delete comment" value="' . $record['id'] .'" name="deletecommentbutton" type="submit">X</button></span>';
}
if(isset($_POST['deletecommentbutton']))
{
$commentid = $record['id'];
$result_delete = mysql_query("DELETE from comments WHERE id='$commentid'");
header("Location: url/discussion_view.php?id=".$_GET['id']."");
}
?>
<div><? echo $record['comment'] ?></div></form>
<?php } ?>
首先,您不应再使用mysql_*
函数。 不推荐使用。
相反,您可以使用PDO 。
关于您的问题,对于给定帖子的每个评论,语句if(isset($_POST['deletecommentbutton']))
为true
,因为您没有检查所选评论的ID。 您应该将删除代码移出循环,并仅对选定的ID执行删除代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.