Welcome Guest, Not a member yet? Register   Sign In
Incremental SET with active query not working
#1

[eluser]Dandy_andy[/eluser]
Ok, so where am I going wrong here... I just want to increment a value by 1 but it doesn't seem to work. I've checked all field names and all is correct. I'm using two WHERE statements - is that where the problem is?

Code:
$this->db->set('read', 'read + 1');
$this->db->where('section_id', $section_id);
$this->db->where('ID', $post_id);
$this->db->update('forum_posts');

I've checked the values of $section_id and $post_id and both are returning the correct numeric value. But the column READ isn't being updated in my DB... Any ideas? Thx.
#2

[eluser]PhilTem[/eluser]
Third argument to set function call must be FALSE to avoid auto-escaping Wink
Code:
$this->db->set('read', 'read + 1', FALSE);

Source here
#3

[eluser]Dandy_andy[/eluser]
Thanks, but when I did that, I got an SQL syntax error... "read + 1 WHERE `section_id` = `1` AND `ID` = `6` at line 1"

UPDATE `forum_posts` SET `read` = read + 1 WHERE `section_id` = `1` AND `ID` = `6`

However, I could see that the "read + 1" syntax wasn't escaped so by adding `` around the last read, it seems to have solved the issue.
#4

[eluser]smilie[/eluser]
In you case, I would recommend:

Code:
$this->db->set('read = read + 1', '', FALSE);

Cheers,
Smilie
#5

[eluser]Dandy_andy[/eluser]
Ah, that makes sense! Thank you!




Theme © iAndrew 2016 - Forum software by © MyBB