Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/264.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP/CodeIgniter-通过数据数组基于多个WHERE条件删除记录_Php_Mysql_Codeigniter - Fatal编程技术网

PHP/CodeIgniter-通过数据数组基于多个WHERE条件删除记录

PHP/CodeIgniter-通过数据数组基于多个WHERE条件删除记录,php,mysql,codeigniter,Php,Mysql,Codeigniter,我正在为我的项目使用PHP/CodeIgniter,我正在试图找出处理此查询的最佳方法 我有一个数据数组,我想用它作为删除记录的WHERE子句 Array ( [0] => Array ( [CustomerID] => 8 [Denomination] => 40 ) [1] => Array ( [CustomerID] =>

我正在为我的项目使用PHP/CodeIgniter,我正在试图找出处理此查询的最佳方法

我有一个数据数组,我想用它作为删除记录的
WHERE
子句

Array
(
    [0] => Array
        (
            [CustomerID] => 8
            [Denomination] => 40
        )

    [1] => Array
        (
            [CustomerID] => 9
            [Denomination] => 425
        )

)
我正在尝试这样做:

public function remove_customers_from_order($formData){
        $this->db
        ->where($formData)
        ->delete("customer_rewards");
    }
有没有比每次循环并删除更有效的方法
CustomerID
面额
都与my WHERE子句的列名匹配

所需结果:

从CustomerID=8、面额=40的客户奖励中删除;
从CustomerID=9且面额=425的customer_奖励中删除;

错误:


像这样运行时,我遇到以下错误:
数组到字符串转换数据库/DB\u query\u builder.php

此查询效率更高:

DELETE 
  FROM customer_rewards 
 WHERE (CustomerID = 8 AND Denomination = 40)
    OR (CustomerID = 8 AND Denomination = 425);

您能想出如何相应地调整循环吗?

此查询更有效:

DELETE 
  FROM customer_rewards 
 WHERE (CustomerID = 8 AND Denomination = 40)
    OR (CustomerID = 8 AND Denomination = 425);
I think you can use:
foreach($formData as $w){
    $this->db->or_where("(`CustomerID` = '".$w['CustomerID']."' AND `Denomination`='".$w['Denomination'].')",NULL,false);
}
$this->db->delete();

你能想出如何相应地调整你的循环吗?

尽管它已经有了一个公认的答案-在CI中这样做的正确方法是:

I think you can use:
foreach($formData as $w){
    $this->db->or_where("(`CustomerID` = '".$w['CustomerID']."' AND `Denomination`='".$w['Denomination'].')",NULL,false);
}
$this->db->delete();
$arrData = [
    [
        'CustomerID' => 8,
        'Denomination' => 40
    ],
    [
        'CustomerID' => 9,
        'Denomination' => 425
    ]
];

foreach($arrData AS $arrItem)
{
    $this->db->or_group_start();
        $this->db->where('CustomerID', $arrItem['CustomerID']);
        $this->db->where('Denomination', $arrItem['Denomination']);
    $this->db->group_end();
}
$this->db->delete('customer_rewards');
在这种情况下,您可以免受SQL注入的影响


尽管已经有了公认的答案,但在CI中,正确的做法是:

$arrData = [
    [
        'CustomerID' => 8,
        'Denomination' => 40
    ],
    [
        'CustomerID' => 9,
        'Denomination' => 425
    ]
];

foreach($arrData AS $arrItem)
{
    $this->db->or_group_start();
        $this->db->where('CustomerID', $arrItem['CustomerID']);
        $this->db->where('Denomination', $arrItem['Denomination']);
    $this->db->group_end();
}
$this->db->delete('customer_rewards');
在这种情况下,您可以免受SQL注入的影响


对在循环内执行查询永远不会失败efficient@Strawberry-这就是我想弄明白的。我没有采用循环方法,试图通过数组,但遇到了一些问题。根据
CodeIgniter
docs,无法像插入多条记录那样一次删除多条记录。是的。在循环内执行查询永远不会失败efficient@Strawberry-这就是我想弄明白的。我没有采用循环方法,试图传递数组,但遇到了一些问题。根据
CodeIgniter
docs,无法像插入多条记录那样一次删除多条记录。我的问题是customerID+面额可以是任何东西。因此,需要找到并删除客户ID和面额的组合。他们两个都是8岁,正好是我的榜样。这是一个糟糕的榜样。EditedMy的问题是customerID+面额可以是任何东西。因此,需要找到并删除客户ID和面额的组合。他们两个都是8岁,正好是我的榜样。这是一个糟糕的榜样。编辑这太棒了,很高兴知道你能做这样的事情!这太棒了,很高兴知道你能做这样的事情!