如何通过复选框删除多个MySQL条目?

时间:2013-11-09 04:47:43

标签: php ajax checkbox sql-delete

在我的应用程序中,我在表格中显示数据库内容。对于显示的每一行,我在行的末尾添加一个复选框:

echo '<td><input type="checkbox" name="ticked[]"></td>';

当用户检查了他们希望删除条目的多个框时,他们点击此删除按钮(前端是zurb基础框架):

<a href="#" class="button radius expand" id="deleteUrl" name="deleteUrl" onClick="deleteUrl('deleteUrl');return false;">Delete URL</a>

按下此按钮时,将触发deleteUrl ajax功能:

function deleteUrl(str)
    {
    document.getElementById("content01").innerHTML="";
    if (str=="")
    {
    document.getElementById("content01").innerHTML="";
    return;
    } 
    if (window.XMLHttpRequest)
    {// code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
    }
    else
    {// code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=function()
    {
    if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("content01").innerHTML = xmlhttp.responseText;
    }
    }
    xmlhttp.open("GET","deleteUrl.php?deleteUrl="+str,true);
    xmlhttp.send();
    document.getElementById("content02").innerHTML = 'Your URL was successfully deleted!<br/><br/>';
    xmlhttp.onreadystatechange = urlRefresh;
    return;
    }

ajax函数将进程定向到我的deleteUrl.php文件:

<!--Include Database connections info-->
<?php include('config.php'); ?>

<?php

    $deleteUrl = $_GET ['$deleteUrl'];

    if(isset($_GET['delete']))  {
        if(is_array($_GET['url']))  {
            foreach($_GET['url'] as $id)    {
                $query = "DELETE FROM contact WHERE url=". $url;
                mysql_query($query)or die(mysql_error());    
            }
        }  
    }

mysql_close();

?>

到目前为止,该过程贯穿始终,没有错误。但是,在此过程中不会删除已检查的条目。

问题:我需要做什么才能使用复选框使删除过程正常工作?

编辑代码:

function runDelete(str, id)

xmlhttp.open("GET","deleteUrl.php?deleteUrl="+str+"&ticked="+id,true);

<a href="#" class="button radius expand" id="deleteUrl" name="deleteUrl" onClick="runDelete('deleteUrl', id);return false;">Delete URL</a>

echo '<td><input type="checkbox" name="ticked[]" value="'.$row['id'].'"></td>';

2 个答案:

答案 0 :(得分:1)

Corey,它只是建议不是你的查询的确切答案。您应该尝试在代码中进行一些修正,如下面的步骤。

首先,您需要将值分配给复选框,如

echo '<td><input type="checkbox" name="ticked[]" value="'.$id.'"></td>';// $id it would different in your case

通过函数调用

传递复选框值
onClick="deleteUrl('deleteUrl',checkboxvalue);

并相应地修改功能

function deleteUrl(str,checkboxvalue)

将复选框值传递给删除网址

xmlhttp.open("GET","deleteUrl.php?deleteUrl="+str+"&ticked="+checkboxvalue,true);

修改删除页面,根据您的复选框值而不是网址删除记录,并确保从ajax传递正确的值并在删除页面上获取正确的值。

答案 1 :(得分:1)

你能试试吗,

1步 - 在头标记中包含jquery网址

2步 - 在jquery url之后包含此代码,

<script type="text/javascript">
   $(function(){

        $("#deleteUrl").click(function(){
            $('#content02').html('');
                var tickedItems = $('input:checkbox[name="ticked[]"]:checked')
                   .map(function() { return $(this).val() })
                   .get()
                   .join(",");

                   $.ajax({
                        type: "POST",
                        url: "deleteUrl.php",   
                        data: "ids=" + tickedItems,                                        
                        success: function(msg) {

                             $('#content02').html('Your URL was successfully deleted!');

                          }             

                    });

                    return false;
        });
    });

    </script>

3步 - 在deleteUrl.php中替换此代码,

   <!--Include Database connections info-->
    <?php include('config.php'); ?>

    <?php

        $deleteUrl = $_GET ['$deleteUrl'];

        if(isset($_POST['ids']))  {

           $idsArray = @explode(',', $_POST['ids']);               
                foreach($idsArray as $id)    {
                    $query = "DELETE FROM contact WHERE url='".$id."' ";
                    mysql_query($query)or die(mysql_error());    
                }

        }

    mysql_close();

    ?>

4步骤 - 将id / property行值分配到复选框

      <?php 
        echo '<td><input type="checkbox" name="ticked[]" value="'.$row['id'].'" ></td>';            
      ?>

5步骤 - 添加此按钮以执行删除操作

<button class="button radius expand" id="deleteUrl" name="deleteUrl" >Delete URL</button>
相关问题