如何使删除按钮工作?

时间:2015-04-24 20:50:16

标签: php mysqli

我正在尝试制作删除按钮,但它无法正常工作。

$interogare = "SELECT * FROM comments JOIN users ON users.user_id = comments.user_id WHERE movie_id='$movie_id' ORDER BY date_posted DESC";
    $result = mysqli_query($dbc, $interogare) or die(mysqli_error($dbc));
    while($rand = mysqli_fetch_assoc($result))
    { 
         echo '<a href="film.php?com='.$rand['comment_id'].'">Delete</a>';

         if(isset($_GET['com'])) {

            $haidi = mysqli_real_escape_string($dbc,$_GET['com']);
            $sql_del = "DELETE FROM comments WHERE comment_id = '$haidi'";
            mysqli_query($dbc,$sql_del);
            header('location: film.php?id='.$_GET['id'].'');
            exit();
         }

     }

当我点击删除链接时,它会将我带到

  

film.php COM =&#39;?$兰特[&#39; COMMENT_ID&#39;]&#39;

页面但没有任何反应,它应该删除我的评论并带我回到评论所在的页面。有人可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

这有用吗?

<?php
$query = "
        SELECT * 
        FROM comments 
        JOIN users ON users.user_id = comments.user_id 
        WHERE movie_id=" . $movie_id  . " 
        ORDER BY date_posted DESC";

$result = mysqli_query($dbc, $query) or die(mysqli_error($dbc));

if (isset($_GET)) {
    $getData = $_GET;
}

while ($rand = mysqli_fetch_assoc($result))
{
    echo '<a href="film.php?com=' . $rand['comment_id'] . '">Delete</a>';

    if (isset($getData) && $getData['com']) {

        $id = mysqli_real_escape_string($dbc, $getData['com']);

        $query = "
            DELETE FROM comments 
            WHERE comment_id=" . $id;

        $result = mysqli_query($dbc, $query);

        if($result)
        {
            header('location: film.php?id=' . $id . '');
        }
        exit();
    }
}

我宁愿抓住帖子一次,然后确保我们传递相同的对象,所以我将GET设置为var。然后,我不喜欢在查询中设置ID的方式,因为它不容易看到。然后我得到了你创建标题的方式,看起来你正在从一些GET数据传递id,而不是你刚提取的id并设置为varibale,见上面......,或者是你的实际上是打算使用$ _GET ['id']呢?

另外,您可能需要在mysqli_real_escape_string()方法中传递数据链接