【问题标题】:Problem in updating multiple row with multiple condition in CodeIgniter在 CodeIgniter 中使用多个条件更新多行时出现问题
【发布时间】:2020-08-24 19:21:03
【问题描述】:

我正在尝试在 Codeigniter 中进行多行更新。我收到一个错误:

A Database Error Occurred
One or more rows submitted for batch updating is missing the specified index.

我认为问题出在模型内部。我将模型功能放在下面,请帮助我解决我做错的事情。

public function updateContacts($client_id){

    $data = array(
              array('address' => 'My address' ,
                    'name' => 'My Name 2' ,
                    ),
              array('address' => 'Another address' ,
                    'name' => 'Another Name 2' ,
                   )
     );
    $multipleWhere = ['tbl_contact.owner_id' => $client_id, 'tbl_contact.owner_type' => '3'];
    $this->db->update_batch('tbl_contact', $data, $multipleWhere);
}

【问题讨论】:

标签: php sql codeigniter model-view-controller


【解决方案1】:

第三个参数应该是官方文档中的key,而不是where条件

The first parameter will contain the table name, the second is an associative array of values, the third parameter is the where key.

但是尝试如下

   $data = array(
              array('address' => 'My address' ,
                    'name' => 'My Name 2' ,
                    ),
              array('address' => 'Another address' ,
                    'name' => 'Another Name 2' ,
                   )
     );

    $this->db->where('owner_id',$client_id);
    $this->db->update_batch('tbl_contact', $data, 'owner_type');

【讨论】:

  • 您好,谢谢您的回答。我试过这个$this->db->update_batch('tbl_contact', $data, 'key');。但看起来它用我要更新的列的值更新了所有行的列。例如,总共有 12 行。我正在获取 6 行,其中键 = 3。我想将 6 行中的一行更改为原样插入其余 5 行。但是这 5 行也更新为我要更改的那一行的相同值。我希望我能让你明白。情况有点复杂。
猜你喜欢
  • 2017-09-02
  • 1970-01-01
  • 1970-01-01
  • 2018-07-18
  • 1970-01-01
  • 2014-06-20
  • 2020-04-06
  • 2015-09-25
  • 2011-01-13
相关资源
最近更新 更多