首页 > 解决方案 > How to batch update multiple records using CodeIgniter query builder?

问题描述

I have looked at the other answers on this topic and I feel that I am following the documentation, but nonetheless I am not able to update. Below is the code in my model:

$query = $this->db->get();
$data = array();
foreach ($query->result_array() as $l_xAnswer) {
    if ($l_xAnswer['status'] == '1' && $l_xAnswer['client_id'] == DB_CLIENT_ID) {
        $data[] = [
            'id' => $l_xAnswer['id'],
            'status' => 2
        ];
    }
}

return $this->db->update_batch('quiz_answers', $data, 'id');

The $data object at this point looks as follows (var_dump):

array(12) {
  [0]=>
  array(2) {
    ["id"]=>
    string(5) "20223"
    ["status"]=>
    int(2)
  }
  [1]=>
  array(2) {
    ["id"]=>
    string(5) "19165"
    ["status"]=>
    int(2)
  }
  [2]=>
  array(2) {
    ["id"]=>
    string(5) "19129"
    ["status"]=>
    int(2)
  }
}

The selected records are not updating. At this point, I feel like I must be missing something obvious. Thank you for looking at this.

标签: phpmysqlcodeignitercodeigniter-3

解决方案


推荐阅读