NoobnSad
NoobnSad

Reputation: 77

How to enable backticks in mysql codeigniter active record

I try codeigniter active record, it give me error when i try to update field named Dec (Jan - Nov was fine before)...

This is my code

        $this->db->set($month,"$month+$tax", FALSE);
        $this->db->set('total_yearly',"total_yearly+$tax", FALSE);
        $this->db->where(array('tax_category' => $tax_category));
        $this->db->update($nama_table); 

And the query return like this

UPDATE `tax` SET Dec = Dec+10000, total_yearly = total_yearly+10000 WHERE `tax_category` = 'Hotel' AND `year` = '2017'

Error message

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Dec = Dec+2479, total_yearly = total_yearly+2479 WHERE tax_category = 'Hotel' AN' at line 1

Any solution? i have tried to add backticks on query, but the result still backtick is removed, or maybe i didnt do it well.

Thanks

Upvotes: 4

Views: 371

Answers (3)

Hasta Dhana
Hasta Dhana

Reputation: 4719

Try replacing :

$this->db->set($month,"$month+$tax", FALSE);

with :

$this->db->set("`$month`","`$month`+$tax", FALSE);

to force adding the backticks

Upvotes: 1

Pradeep
Pradeep

Reputation: 9707

Hope this will help you :

remove third parameter FALSE to allow use backtick

set() will also accept an optional third parameter ($escape), that will prevent data from being escaped if set to FALSE.

$this->db->set($month,$month.'+'.$tax);
$this->db->set('total_yearly',"total_yearly+".$tax);
$this->db->where(array('tax_category' => $tax_category));
$this->db->update($nama_table); 

read more : https://www.codeigniter.com/user_guide/database/query_builder.html#updating-data

Upvotes: 2

wordragon
wordragon

Reputation: 1357

I think the problem is that "DEC" is a reserved word. Throw some backticks around the field names in your SET clause so the query reads:

UPDATE `tax` SET `Dec` = `Dec`+10000, `total_yearly` = `total_yearly`+10000 WHERE `tax_category` = 'Hotel' AND `year` = '2017'

Upvotes: 1

Related Questions