CodeIgniter Forums
update('value', 'value' + 1) - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: update('value', 'value' + 1) (/showthread.php?tid=11535)



update('value', 'value' + 1) - El Forum - 09-12-2008

[eluser]eldiablo[/eluser]
hi,
i want to add one the a value in the db
this work in start mysql terms
mysql_query("UPDATE mytable SET `value`=`value`+50 WHERE `id`="$userId);

i have tried update('value', 'value' + 1) and various other variations
and that doesn't work
anyone have any suggestions

thanks


update('value', 'value' + 1) - El Forum - 09-12-2008

[eluser]Bramme[/eluser]
It's not working because you haven't read the manual properly.

Try
Code:
$this->db->update('mytable', array('value' => 'value + 1'));
Though that might get escaped and thus not executed right. You might have to use a full query.

Code:
$this->db->query("UPDATE mytable SET `value`=`value`+50 WHERE `id`="$userId");



update('value', 'value' + 1) - El Forum - 09-12-2008

[eluser]xwero[/eluser]
Escaping the field name is the cause of the problem so if you add value=value+50 to the sql statement it will be fine. As an alternative you can use the AR set method.
Code:
$this->db->set('value','value+50',FALSE)->where('id',$userid)->update('mytable');



update('value', 'value' + 1) - El Forum - 09-12-2008

[eluser]eldiablo[/eluser]
Hi,
thanks for the replies
i got these to work:
$this->db->query("UPDATE mytable SET `value`=`value`+1 WHERE `id`=".$id);

and

$this->db->where('id', $id);
$this->db->set('value', 'value+ 1',false);
$this->db->update('mytable');

thanks guys


update('value', 'value' + 1) - El Forum - 09-12-2008

[eluser]Bramme[/eluser]
With pleasure.

Must admit I like xwero's code better though. Chaining is so pretty. PHP5 only though!


update('value', 'value' + 1) - El Forum - 09-25-2008

[eluser]sikkle[/eluser]
Hey Xwero,


$this->db->set('value','value+1',FALSE)->where('id',$userid)->update('mytable');

I have a weird issue with that, it's seem that the system is trying to do +2.

Indeed if i go directly insire phpmyadmin and do UPDATE table SET value = value+1 where id = 1;

that work pretty well.

i use the 1.7 svn version of ci, if someone got two minute to give a test.

thanks !


update('value', 'value' + 1) - El Forum - 09-25-2008

[eluser]xwero[/eluser]
I never had problem with it and i don't think there are changes in the AR library as far as i know.


update('value', 'value' + 1) - El Forum - 09-25-2008

[eluser]sikkle[/eluser]
Ok i will try to keep debugging it, i doesnt seem to understand it so far.

nowaday, thanks !


update('value', 'value' + 1) - El Forum - 09-29-2008

[eluser]sikkle[/eluser]
baaah, sometime you just work too much Smile

thanks guys, my bad all the way..