Welcome Guest, Not a member yet? Register   Sign In
update('value', 'value' + 1)
#1

[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
#2

[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");
#3

[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');
#4

[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
#5

[eluser]Bramme[/eluser]
With pleasure.

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

[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 !
#7

[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.
#8

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

nowaday, thanks !
#9

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

thanks guys, my bad all the way..




Theme © iAndrew 2016 - Forum software by © MyBB