无法使用ajax函数从数据库中删除行

时间:2014-02-28 13:46:40

标签: php jquery mysql sql ajax

我有记录列表。我希望通过单击来删除记录,但是当我使用此$.ajax()函数时,它会删除该行,但不会从数据库中删除。

html代码

<tbody>
      <tr class="gradeX odd">
        <td class="sorting_1">2</td>
        <td class=" ">test</td>
        <td class=" ">test</td>
        <td class="center ">0</td>
        <td class="center ">Active</td>
        <td class=" "><button type="button" class="btn btn-default btn-circle" name="editRecord"><i class="fa fa-pencil-square-o"></i></button>
          <button type="button" class="btn btn-danger btn-default btn-circle" id="2" name="deleteRecord"><i class="fa fa-times"></i></button></td>
      </tr>
      <tr class="gradeX even">
        <td class="sorting_1">3</td>
        <td class=" ">sarees</td>
        <td class=" ">contains sarres</td>
        <td cl="center ">1</td>
        <td class="center ">Active</td>
        <td class=" "><button type="button" class="btn btn-default btn-circle" name="editRecord"><i class="fa fa-pencil-square-o"></i></button>
          <button type="button" class="btn btn-danger btn-default btn-circle" id="3" name="deleteRecord"><i class="fa fa-times"></i></button></td>
      </tr>

    </tbody>

jquery的

$(document.body).on('click', '[name="deleteRecord"]', function(){

     var id= $(this).attr('id');
      if (confirm("Are you sure you want to delete this row?"))
        {
            var id = $(this).attr('id');
            var data = 'id=' + id ;
            var parent = $(this).parent().parent();

            $.ajax(
            {
                   type: "POST",
                   url: "admin_operation.php?mode=delete_category",
                   data: data,
                   cache: false,

                   success: function(data)
                   { 
                    console.log(data);  
                    parent.fadeOut('slow', function() {$(this).remove();});
                   }
             });
        }

 });

php代码

$mode = $_GET['mode'];
if($mode=='delete_category')
{    

   $id=$_POST['id'];
   $q=$db->query("DELETE FROM db_category WHERE category_id='".$id."'");
   if($q){ echo "yes";}
   else{ echo "no";}

}

3 个答案:

答案 0 :(得分:0)

Ajax(模式和id为get vars):

$.ajax(
        {
               type: "GET",
               url: "admin_operation.php?mode=delete_category&id="+id, 
               cache: false,
               success: function(data)
               { 
                console.log(data);  
                parent.fadeOut('slow', function() {$(this).remove();});
               }
         });

腓:

$mode = $_GET['mode'];
if($mode=='delete_category')
{    

$id=$_GET['id'];
$q=$db->query("DELETE FROM db_category WHERE category_id='".$id."'");
if($q)
{ echo "yes";
}
else
{ echo "no";
}

}

答案 1 :(得分:0)

使用变量“data”发送参数“mode”,并仅使用$ _POST来检索值。因此,参数“URL”将只有值“admin_operation.php”,变量“data”将包含:

var data = 'id =' + id + '&mode=delete_category';

示例:

        var id = $(this).attr('id');
        var data = 'id=' + id + '&mode=delete_category';
        var parent = $(this).parent().parent();

        $.ajax(
        {
               type: "POST",
               url: "admin_operation.php",
               data: data,
               cache: false,

               success: function(data)
               { 
                console.log(data);  
                parent.fadeOut('slow', function() {$(this).remove();});
               }
         });

答案 2 :(得分:-1)

您告诉PHP从GET和POST获取参数。您的表单正在发送为GET。因此,您的代码不执行SQL语句,因为$ id为空。

您需要将其更改为仅使用GET,因为这是您在表单上发送的内容。

  $id=$_GET['id'];

此外,您的Javascript正在删除该行,因为如果该行已被删除,您未在服务器上进行验证。一定要检查一下。

   $.ajax(
        {
               type: "GET",
               url: "admin_operation.php?mode=delete_category",
               data: data,
               cache: false,

               success: function(data)
               { 
                console.log(data);  
                parent.fadeOut('slow', function() {$(this).remove();});
               }
         });