2012-07-04 221 views
0

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

  • 标识,
  • 名称,
  • 网址,
  • 电子邮件和
  • 身体

我如何编写代码delete.php到与MySQL和评论系统交互?谢谢!

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

POST评论ID为delete.php,然后又传递到您的查询,然后简单地删除了该行与该ID。你在delete.php方面到目前为止有什么? –

回答

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. 
?> 
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(delete.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";