简体   繁体   English

如何使用Ajax从数据库中删除记录

[英]how to delete records from database with an Ajax

I am using php,mysql and ajax to delete record from a table. 我使用php,mysql和ajax从表中删除记录。 The problem is that the in MySQL_query it not getting the id it shows "id= undefined", i tried to pass the id to the query but i don't know where i went wrong i tried to print MySQL its shows 问题是在MySQL_query它没有得到它显示“id =未定义”的ID,我试图将id传递给查询,但我不知道我哪里出错了我试图打印MySQL其节目

delete from 9xx WHERE id = undefinedArray
(
    [rowid] => undefined
    [supplier] => 9xx
)

can anyone tell me how to pass the id ...thanks 任何人都可以告诉我如何传递id ...谢谢

My ajax 我的阿贾克斯

$(".deletesuppliernetwork").live('click',function()
        {
         arr = $(this).attr('class').split( " " );
        var supplier=document.getElementById("supplier").value;

        if(confirm("Sure you want to delete this update?"))
        {
        $.ajax({
        type: "POST",
        url: "suppliernetwork/delete.php",
        data: "rowid="+arr[2]+"&supplier="+supplier,
        success: function(data){
                                                         $('.ajax').html($('.ajax input').val());
                                                         $('.ajax').removeClass('ajax');
                                                    }});
        }
        });

My html 我的HTML

<?php
include"db.php";

$supplier_id=$_GET['supplier_id'];

if($supplier_id!=""){

$sql=mysql_query("select * from $supplier_id order by country,networkname" );

while($rows=mysql_fetch_array($sql))
{

if($alt == 1)
        {
           echo '<tr class="alt">';
           $alt = 0;
        }
        else
        {
           echo '<tr>';
           $alt = 1;
        }

echo '  <td style="width:123px" class="edit supplier '.$rows["id"].'">'.$rows["supplier"].'</td>
                <td style="width:104px" class="edit rn '.$rows["id"].'">'.$rows["rn"].'</td>    
            <td style="width:103px" class="edit sc '.$rows["id"].'">'.$rows["sc"].'</td>    
            <td style="width:108px" class="edit comment '.$rows["id"].'">'.$rows["comment"].'</td>

            <td style="width:62px" class="deletesuppliernetwork '.$rows["id"].'"><img   src="/image/delete.png" style="margin:0 0 0 17px" ></td>                                

        </tr>';


}
}
?>

delete.php delete.php

<?php
    include"db.php";

$supplier=$_POST['supplier'];




        $rownum=$_POST['rowid'];  
        $sql="delete from $supplier WHERE id = ".$rownum."";

        print $sql;

        mysql_query($sql);  


    print_r($_POST);
?>
<td style="width:62px" class="deletesuppliernetwork '.$rows["id"].'"><img   src="/image/delete.png" style="margin:0 0 0 17px" ></td>     

the index of your ID is 1, that is second index. 您的ID索引是1,即第二个索引。 not 2. 不是2。

$.ajax({
        type: "POST",
        url: "suppliernetwork/delete.php",
        data: "rowid="+arr[1]+"&supplier="+supplier,
        success: function(data){
                                                         $('.ajax').html($('.ajax input').val());
                                                         $('.ajax').removeClass('ajax');
                                                    }});
var rowObj = $(this);    
$.ajax({
            type: "POST",
            url: "suppliernetwork/delete.php",
            data: "rowid="+arr[1]+"&supplier="+supplier,
            success: function(data){
                 $('.ajax').html($('.ajax input').val());
                 $('.ajax').removeClass('ajax');
                  $(rowObj).parents("tr:first").hide();
            }});

this should hide your complete row. 这应该隐藏你的完整行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM