简体   繁体   English

$ .ajax不会使用复选框删除多个表行

[英]$.ajax does not delete multiple table rows with checkbox

I'm currently struggling as to why my code does not do the following multiple table row deletion php code: 我目前正在努力为什么我的代码不执行以下多表行删除php代码:

incude("dbconnect.php");

if (isset($_POST["id"])) {

    foreach ($_POST["id"] as $id) {

        $sql = "DELETE FROM doctor WHERE DOC_LICENSE_NUM='.$id.'";

        if ($mydatabase->mysqli_query($sql) === TRUE) {
            echo "<div>Record deleted successfully</div>";
        } else {
            echo "Error deleting record: " . $mydatabase->error;
        }
        //$sql="DELETE from DOCTOR where DOC_LICENSE_NUM='".$id."'";
        //mysqli->query($connect, $sql);
    }
    //mysqli->close();
}

whereas dbconnect links $mydatabase to the sql. 而dbconnect将$ mydatabase链接到sql。 Now, I created a button with the id btn_delete in order to use javascript click function in handling multiple rows to delete in my sql. 现在,我创建了一个ID为btn_delete的按钮,以便使用javascript click函数来处理要在我的sql中删除的多行。

<div style="float: right; margin: 0px 10px 10px 0px;"><button type="button" name="btn_delete" class="btn" id="btn_delete" ><span class="fa fa-trash" style="font-size:16px; margin-right:5px;"></span>Delete Multiple</button></div>

What I did is that I used $.ajax in order to gather the checked checkboxes in an array and using the url for my php for deleting rows in the table and if it has entered the php it will proceed to fade the rows out of the table. 我所做的是我使用$ .ajax来收集数组中选中的复选框,并使用我的php的url删除表中的行,如果它已进入php,它将继续淡出行。表。

$('#btn_delete').click(function(){
    if(confirm("Are you sure you want to delete these?")){
        var id=[];
        $(':checkbox:checked').each(function(i){
            id[i]=$(this).val();
            //alert(id);
        });

        if(id.length===0){
            alert("Please select at least two checkboxes");
        }else{
            $.ajax({
                url:'muldel.php',
                method: 'POST',
                data: {id:id},
                success: function(){
                    for (var i=0; i<id.length; i++){
                        $('tr#'+id[i]+'').css('background-color', '#ccc');
                        $('tr#'+id[i]+'').fadeOut('slow');
                    }
                    //location.reload();
                }

            });
        }
    }else{
        return false;
    }
});

However, when it proceeds to fade the row out of my tables, after I refresh it, it does not delete the rows like how I expected it to. 但是,当它继续淡出表中的行时,刷新后,它不会像我期望的那样删除行。 What am I missing out here? 我在这里错过了什么? (I forgot to mention that I used a unique entity key as my id) (我忘了提到我使用唯一的实体密钥作为ID)

Since you are passing an array from your Ajax call, you will need to build a SQL query using that: 由于您要通过Ajax调用传递数组,因此需要使用以下命令来构建SQL查询:

$sql = "DELETE FROM doctor WHERE DOC_LICENSE_NUM IN (" . implode(",", $_POST['id']) . ".)";

// Execute 
 if ($mydatabase->mysqli_query($sql) === TRUE) {
     echo "<div>Record deleted successfully</div>";
 } else {
    echo "Error deleting record: " . $mydatabase->error;
 }

Note: You're open to SQL Injection. 注意:您可以使用SQL注入。 Always use prepared statements when your query requires parameters by others for it to execute. 当查询需要其他参数来执行查询时,请始终使用准备好的语句。 This could be a little help. 可能会有所帮助。

From your jquery you are posting id as coma separated ie. 从您的jquery中,您发布的ID以逗号分隔,即。 1,2,3,4 etc. This should convert to array in php side array(1,2,3,4) using explode(); 1,2,3,4等。这应该使用explode();转换为php侧面array(1,2,3,4) explode(); or make array($_POST["id"]) then put this in your foreach loop. 或制作array($_POST["id"])然后将其放入foreach循环中。

foreach ($array as $id) {

}

Hope this helps 希望这可以帮助

or alternate way without foreach in php, you can use IN() in mysql. 或没有php中的foreach的替代方法,可以在mysql中使用IN() Check complete code if intersted. 如果有兴趣,请检查完整的代码。 http://www.phpzag.com/delete-multiple-rows-with-checkbox-using-jquery-php-mysql/ http://www.phpzag.com/delete-multiple-rows-with-checkbox-using-jquery-php-mysql/

You can use below query to delete multiple records. 您可以使用下面的查询删除多个记录。

$sql = "DELETE FROM doctor WHERE DOC_LICENSE_NUM IN (" . implode(',', $_POST['id']) . ")";
if ($mydatabase->mysqli_query($sql) === TRUE) {
    echo "<div>Record deleted successfully</div>";
} else {
    echo "Error deleting record: " . $mydatabase->error;
}

There is no need to iterate loop. 无需迭代循环。

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

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