Codeigniter: Update batch wrongly escaping causing error

Created on 31 Oct 2016  路  6Comments  路  Source: bcit-ci/CodeIgniter

(Similar bug #4871 I think)

Started since v3.1.2: example code:

$data = array(
array(
'setting_key' => 'test',
'setting_value' => 'test'
)
);
$this->db->where('user_id', 1);
$this->db->update_batch('settingstable', $data, 'setting_key');`

Returning

ERROR - 2016-10-31 10:05:32 --> Severity: Notice --> Undefined index: setting_key system\database\DB_query_builder.php 1949
ERROR - 2016-10-31 10:05:32 --> Severity: Notice --> Undefined index: setting_key system\database\DB_query_builder.php 1955
ERROR - 2016-10-31 10:05:32 --> Severity: Notice --> Undefined index: setting_key system\database\DB_query_builder.php 1955

Line 1949

$ids[] = $val[$index];

The items in $val are escaped and the $index search on non escaped name. I think the keys shouldn't be escaped?

Bug Regression

Most helpful comment

I have found that replacing the $index argument back to $this->protect_identifiers($index) in the update_batch function in DB_query_builder.php on the following line will fix the creation of these batch update SQL statements. I am unsure if this will affect anything else? Can anyone else confirm this?

if ($this->query($this->_update_batch($this->protect_identifiers($table, TRUE, NULL, FALSE), array_slice($this->qb_set, $i, $batch_size), $index)))

https://github.com/bcit-ci/CodeIgniter/blob/84f5366a8d76fe935a57cb9dbf61c970d98a0a27/system/database/DB_query_builder.php#L1918

All 6 comments

I have come across the same issue with v3.1.2

Using the example data below:

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

        $this->db->update_batch('mytable', $data, 'title');

Produces the following incorrectly formed query

UPDATE mytable SET title = CASE WHEN title = THEN 'My title' WHEN title = THEN 'Another title' ELSE title END, name = CASE WHEN title = THEN 'My Name 2' WHEN title = THEN 'Another Name 2' ELSE name END, date = CASE WHEN title = THEN 'My date 2' WHEN title = THEN 'Another date 2' ELSE date END WHERE title IN(,)

Prior to Fix #4871

UPDATE mytable SET name = CASE WHEN title = 'My title' THEN 'My Name 2' WHEN title = 'Another title' THEN 'Another Name 2' ELSE name END, date = CASE WHEN title = 'My title' THEN 'My date 2' WHEN title = 'Another title' THEN 'Another date 2' ELSE date END WHERE title IN('My title','Another title')

I have found that replacing the $index argument back to $this->protect_identifiers($index) in the update_batch function in DB_query_builder.php on the following line will fix the creation of these batch update SQL statements. I am unsure if this will affect anything else? Can anyone else confirm this?

if ($this->query($this->_update_batch($this->protect_identifiers($table, TRUE, NULL, FALSE), array_slice($this->qb_set, $i, $batch_size), $index)))

https://github.com/bcit-ci/CodeIgniter/blob/84f5366a8d76fe935a57cb9dbf61c970d98a0a27/system/database/DB_query_builder.php#L1918

I have the same issue, please fix this as soon as possible

Same here, a fix is more than welcome.
I'll go back to 3.1.0 until this is solved.

Please test with 8338bbb3586e31432caa503b6530dcea583dd8d8

@narfbg just tested, looks good! Thanks

Was this page helpful?
0 / 5 - 0 ratings