0

我在这里做了一个简单的评论系统,我想创建一个删除按钮,如果按下,评论将被删除(基本上从数据库中删除)。我不知道如何在评论中实现这一点。我已经制作了 javascript 来与 delta.php 交互,但我不知道如何制作 delete.php。mySQL 字段是;

  • ID,
  • 姓名,
  • 网址,
  • 电子邮件和
  • 身体

我如何编码 delete.php 以与 mySQL 和评论系统交互?谢谢!

<script type="text/javascript">
    function deleteUser(id){
    new Ajax.Request('delete.php', {
        parameters: $('idUser'+id).serialize(true),
    });
}
</script>
4

2 回答 2

0

这是一个例子:

Javascript

function deleteComment(id)
{
    $.ajax(
    {
        type: "POST",
        url: "delete.php?id="+id,
        dataType: "html",
        success: function(result)
        {
            if(result == "Ok") alert("The comment is successfuly deleted");
            else alert("The following error is occurred: "+result);
        }
    });
}

PHP(删除.php):

if(!isset($_POST['id'])) die("No ID specified");
$id = mysql_real_escape_string($_POST['id']);

mysql_query("DELETE FROM `comments` WHERE `id` = $id") or die(mysql_error());

echo "Ok";
于 2012-07-07T14:43:42.893 回答
0

您可以使用 jquery 使事情变得更容易:

$('#delete_button').click(function(){
   var comment_id = $(this).attr('id');//you should use the id of the comment as id for the delete button for this to work.
   $.post('delete.php', {'comment_id' : comment_id}, function(){
      $(this).parent('div').remove(); //hide the comment from the user
   });
});

delete.php 将包含以下内容:

<?php
//include database configuration file here. 
//If you don't know how to do this just look it up on Google.

$comment_id = $_POST['comment_id'];

mysql_query("DELETE comment from TABLE WHERE comment_id='$comment_id'");
//mysql_query is not actually recommended because its already being deprecated. 
//I'm just using it for example sake. Use pdo or mysqli.  
?>
于 2012-07-07T14:37:49.217 回答