删除您使用php代码发布的评论?

时间:2011-10-13 15:17:03

标签: php sql database

我想知道你是否可以帮助我...我创建了一个链接共享网站并设法在共享链接上创建评论。

我想给你一个我想要实现的方案。例如,user_1发出的每条评论都只能由user_1和admin删除。

据我所知,当从php页面“删除”它时,它也必须从数据库中删除。你怎么能这样做?

//I pressume where I INSERTED my post's 'vales' I must DELETE them again from there??
//It is very much alike from reply.php's code where you INSERT the data into the database. Now I just want to delete it.
//I don't know if this code below is correct??

$sql = "DELETE FROM 
            posts(post_content,
                  post_date,
                  post_topic,
                  post_by) 
        WHERE ('" . $_POST['reply-content'] . "',
                NOW(),
                " . mysql_real_escape_string($_GET['id']) . ",
                " . $_SESSION['user_id'] . ")";


$result = mysql_query($sql);

if(!$result)
{
    echo 'Your reply has not been saved, please try again later.';
}
else
{
    echo 'Your comment has been deleted!';
}

3 个答案:

答案 0 :(得分:0)

您应该提供您的评论ID,然后执行delete from posts where ID = $id

您当前拥有的SQL语句甚至不会执行。请查看manual语法的工作原理。

答案 1 :(得分:0)

您的删除查询存在重大语法错误。您不会从表中删除单个字段 - 您不能。你只能删除整个记录。正确的语法是:

DELETE FROM sometable WHERE (...)

您的where子句也有错误。您没有进行任何比较操作,只列出了一些值。再次,语法错误。最喜欢你想要这个(猜你的帖子的主键字段名称):

DELETE FROM posts WHERE (post_id = $id);

答案 2 :(得分:0)

真正有用的是将表示逻辑与db逻辑从总线中分离出来。逻辑。尝试MVC模式,这使得解析代码和仅查看数据库或演示文稿或总线变得更加容易。逻辑代码。然后,我们可以专注于发布问题的答案。