Welcome Guest, Not a member yet? Register   Sign In
sql value update problem
#1

[eluser]Unknown[/eluser]
First of all sorry for my bad english.

I have a problem increasing value in sql table.

Table column type:

int(3)

Code is:

Code:
$this->db->query("UPDATE table SET field=field+1 WHERE x=1 LIMIT 1");

It is increasing 3 times. Example: value = 1, run the code, result = 4.

What is the problem, please help me.
#2

[eluser]LuckyFella73[/eluser]
Your code makes not clear what you want to do. Is "x" some kind
of "id" column?

If you want to increase one single row you better write somthing like:
Code:
$this->db->query("UPDATE table SET field=field+1 WHERE id=123");

If you want to increase ALL rows with condition x=1 you write somthing like:
Code:
$this->db->query("UPDATE table SET field=field+1 WHERE x=1");

Quote:It is increasing 3 times. Example: value = 1, run the code, result = 4.
Could you post more code and what do you mean with "value" -> the "1" in "x=1"?
The single line doesn't explain why you didn't get the desired update, if
you post more fo your code it would be easier to help you with this problem.
#3

[eluser]Erk[/eluser]
Hi SeVeRaL

Did you find the solution to this problem?

I'm also getting the same issue where I'm trying to increment my Page Counter to see how many visitors have visited a page, however, every time I run the query in CodeIgniter it increases 3 number at a time.

this is the line where I'm running the query:
Code:
$this->db->simple_query("update `page` set `page_views`=`page_views`+1 where id='18'");

Running this line makes the page_views value jump from 0 to 3 as opposed to 0 to 1 as expected.

Thanks

Eric Boisjoli
Erk's Werks Custom Software
Get your Community Online
#4

[eluser]LuckyFella73[/eluser]
Hi Erk,

try this one:
Code:
$this->db->set('page_views', 'page_views+1',FALSE);
$this->db->where('id',18); // hardcoded 18 for your example
$this->db->update('page');

should work (I hope)
#5

[eluser]Erk[/eluser]
That didn't seem to work, I'm starting to suspect that my page may run twice for each page refresh, (not sure how) but I'm looking into it.
#6

[eluser]Erk[/eluser]
OK yea, I was right, every time i was running a page it was trying to include a CSS file and when it couldn't be found was triggering CI and incrementing the page count.

Thanks for your help!




Theme © iAndrew 2016 - Forum software by © MyBB