简体   繁体   English

MYSQL删除特定的行按钮PHP

[英]MYSQL delete specific row button PHP

I am building a very basic forum with the ability to comment on posts. 我正在建立一个非常基本的论坛,可以对帖子发表评论。 As you can see, this code shows the commments and when the comment author is equal to the logged in user, it should show a delete button to delete the comment. 如您所见,此代码显示注释,当注释作者等于登录用户时,它应显示一个删除按钮以删除注释。 The problem is now that when I click the delete button, it deletes all the comments on the post and not only the comment with the delete button next to it. 现在的问题是,当我单击“删除”按钮时,它将删除帖子上的所有评论,而不仅是旁边带有删除按钮的评论。

My 'comments' table has the columns id, discid, comment_username, comment and comment_time in which the discid column from 'comments' refers to the id column in the table 'dicussions' 我的“ 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 } ?>

First of all you should not use mysql_* functions anymore. 首先,您不应再使用mysql_*函数。 It's deprecated. 不推荐使用。

Instead you can use PDO . 相反,您可以使用PDO

And about your problem, statement if(isset($_POST['deletecommentbutton'])) is true for every comment on given post because you are not checking ID of selected comment. 关于您的问题,对于给定帖子的每个评论,语句if(isset($_POST['deletecommentbutton']))true ,因为您没有检查所选评论的ID。 You should move deletion code outside of loop and execute it only for selected ID. 您应该将删除代码移出循环,并仅对选定的ID执行删除代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM