如何删除用逗号分隔的行?

时间:2014-09-08 05:43:23

标签: php mysql sql sql-delete

嗨,我有一个简单的问题。我可以从网站的url获取id并通过在php中使用$_GET['']函数来获取该id。我的链接如下:.php?id=10,11,&from=user。我想知道的是,如果有一个删除sql用于用户的id。

if($_POST['action']){
      $id = $_GET['id'];
      $from = $_GET['from'];

      if(!empty($id))
      {
            $statement = " <-- SQL STATEMENT --> ";
            $result = mysql_query($statement);

            if($result)
            {header("location:user.php");}
      }

修改 好的。我的表是这样的:

ID, Username, Password, Role     Checkbox
1    test       test    Admin       []
2    branch     branch  Manager     []
3    hello      hello   Manager     []

我有一个复选框,我使用javascript中的函数获取这些值

    var id="";
    $("#form1 input:checkbox:checked").each(function(){
        id = $(this).data('id')+' '+id;
    });

    $.pgwModal({
            url: 'modaldeleteuser.php?id='+id,
            modalData : {
                id : ''+id
            },
            loading: '<span style="text-align:center">Loading in progress</span>',
            title: 'Delete User',
            close: false

        });

我使用模态脚本通过URL将值传递给modaldelete.php,将其传递给php变量

    <div>
        <?php 
            $id = $_GET['id'];
        ?>

    <form action="submit.php?id=<?php echo $id;?>&from=user" method="post" enctype="multipart/form-data">
        <p class="text-center">Are you sure you want to delete user(s) <?php echo $_GET['id'];?> from user table?</p>
        <!-- <p id ="demo" class="text-center"></p> -->
        <p class="text-center"><input type="hidden" name="action" value="delete"/>
       <button class="btn btn-danger">Delete</button> &nbsp; <a href="javascript:void(0)" class="btn btn-inverse" onclick="$.pgwModal('close');">Cancel</a></p>
    </form>
    </div>

所以对于submit.php

    if($_POST['action']=="delete") {
        if(!empty($_GET['id']))
        {
        $statement = "DELETE FROM user WHERE `Employee_ID` IN (" . implode(", ",$_GET['id']) . ")";
        $result = mysqlparser($statement, $db);
        header("location:user.php?delete=1");   
        }
        else
        {header("location:user.php?delete=2");  }
    }

所以现在。我再次提出问题..如何从URL中删除以逗号分隔的ID?

1 个答案:

答案 0 :(得分:2)

$from = mysql_real_escape_string($_GET['from']); // Protect against SQL injection
$ids = implode(',', array_map('intval', explode(',', $_GET['id'])));

$statement = "DELETE FROM yourtable 
              WHERE from = '$from' AND id IN ($ids)";