简体   繁体   English

如何使用PHP中的ID数组从mysql表中删除多行

[英]How to remove multiple rows from mysql table, using an array of IDs in PHP

I'm having trouble using an array to make an sql query delete multiple rows of data at once. 我在使用数组使sql查询一次删除多行数据时遇到麻烦。 Please ignore that I'm using sql injection for now. 请忽略我现在正在使用sql注入。

EDIT: A somewhat better explanation - my php does not work at all. 编辑:更好的解释-我的php根本不起作用。 I have edited it somewhat, after checking the link Sebastian provided in the comments 在检查评论中提供的链接塞巴斯蒂安之后,我已经对其进行了一些编辑

NOTE: I'm using AngularJS to post 注意:我正在使用AngularJS发布

This is my $data structure, from my controller.js function. 这是我的controller.js函数中的$ data结构。 devicesToDelete is just a simple array, eg ["1", "2", "3"]: devicesToDelete只是一个简单的数组,例如[“ 1”,“ 2”,“ 3”]:

$http.post('./js/removeMultiple.php', { 'devicesToDeleteArray': devicesToDelete })
     .success(function(data) {
          $scope.results = data;
          devicesToDelete = [];
     })
     .error(function(err) {
         $log.error(err);
     })

This is the updated php code im having trouble with. 这是更新的PHP代码,我遇到了麻烦。 On clicking my deleteMultiple button, only the confirm alert fires, the selected items aren't deleted and the console is clear of errors: 在单击我的deleteMultiple按钮时,仅会触发确认警报,不会删除所选项目,并且控制台中没有错误:

<?php

$data = json_decode(file_get_contents("php://input"));

include('config.php');

$sql = "DELETE FROM devices WHERE devID in ( implode(",", $data) )";

$qry = $conn->query($sql);

$sql = "SELECT * FROM devices";

$qry = $conn->query($sql);

$data = array();

if($qry->num_rows > 0){
    while($row = $qry->fetch_object()){
        $data[] = $row;
    }
}else {
    $data[] = null;
}




$conn->close();

echo json_encode($data);

EDIT 2: I managed to solve the issue on my own. 编辑2:我设法自己解决了这个问题。 Here is the solution: 解决方法如下:

I used .join() on my array on the javascript side to have all of the IDs for deletion be separated by commas (eg 1, 2, 3): 我在JavaScript端的数组上使用了.join(),所有要删除的ID都用逗号分隔(例如1、2、3):

$http.post('./js/removeMultiple.php', { 'devicesToDeleteArray': devicesToDelete.join(", ") })
                    .success(function(data) {
                        $scope.results = data;
                        devicesToDelete = [];
                    })
                    .error(function(err) {
                        $log.error(err);
                    })

Then I just added what I completely glanced over in my php code which is the name of the array i was passing into it - devicesToDeleteArray 然后我只添加了我在php代码中完全浏览过的内容,这就是我要传递给它的数组的名称devicesToDeleteArray

<?php

$data = json_decode(file_get_contents("php://input"));

include('config.php');

$sql = "DELETE FROM devices WHERE devID in ($data->devicesToDeleteArray)";

$qry = $conn->query($sql);

$sql = "SELECT * FROM devices";

$qry = $conn->query($sql);

$data = array();

if($qry->num_rows > 0){
    while($row = $qry->fetch_object()){
        $data[] = $row;
    }
}else {
    $data[] = null;
}

$conn->close();

echo json_encode($data);

here is php code: 这是PHP代码:

<?php
$ids = $_POST['devicesToDeleteArray'];
if(!is_array($data)) {
   $ids = json_decode($data, true);
}
if(!empty($ids)) {
include 'config.php';

$sql  = "DELETE FROM devices WHERE devID in (implode(",", $ids))";
$qry  = $conn->query($sql);
$sql  = "SELECT * FROM devices";
$qry  = $conn->query($sql);
$data = array();
if ($qry->num_rows > 0) {
    while ($row = $qry->fetch_object()) {
        $data[] = $row;
    }
} else {
    $data[] = null;
}
$conn->close();
echo json_encode($data);

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

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