Welcome Guest, Not a member yet? Register   Sign In
How to UPDATE DB values using active record?
#1

[eluser]pernavoi[/eluser]
What is active record syntax for following query? amount1 and amount2 are INT values and I want to add values to them from data array

Code:
$this->db->query("UPDATE mytable
SET amount1 = amount1 + '{$data['amount1']}', amount2 =  amount2 + '{$data['amount2']}'
WHERE id = '{$data['id']}'");
#2

[eluser]techgnome[/eluser]
It's all in the User Guide... all you have to do is click and read - http://ellislab.com/codeigniter/user-gui...tml#update

-tg
#3

[eluser]pernavoi[/eluser]
I was just blind, didn't notice that how to use SET for adding values on first couple of reads.

So it's this simple:

Code:
$this->db->set('amount1', 'amount1' + $data['amount1'] );
$this->db->set('amount2', 'amount2' + $data['amount2'] );
#4

[eluser]pernavoi[/eluser]
But it still doesn't work......

Code:
$this->db->set('field', 'field' + $data['field'] );
$this->db->where('id', $data['id']);
$this->db->update('table');

This gives:
UPDATE `table` SET `field` = 1 WHERE `id` = '2'

While it should be:
UPDATE `table` SET field = field + '1' WHERE `id` = '2'


EDIT - Finally solved:
Pretty basic PHP :red:
Code:
$this->db->set('field', 'field + '.$data['field'].'', FALSE);




Theme © iAndrew 2016 - Forum software by © MyBB