-1

This code iv written is working as i like, but for some reason when refreshing the page counter adds 2 instead of 1, the Views column is as an Integer.

$updateviews = $db->prepare("UPDATE pages SET views = views + 1 WHERE type = 'home'");
$updateviews->execute();

Hope someone can help, cant seam to find the problem.

  • 1
    Are you sure in this? If the value of this on first load is 1, then on refresh should be 2, then 3, etc.. – vaso123 Nov 12 '14 at 15:51
  • What you've got here should work the way you described. – txyoji Nov 12 '14 at 15:53
  • Yes, its adding 2 each time somehow. I will put the code on a separate table and such as test it totally seperately from the project im working on. – haydenbarton96 Nov 12 '14 at 15:53
  • 3
    You are missing the WHERE clause. See this: http://stackoverflow.com/questions/2762851/increment-a-database-field-by-1 – Grasshopper Nov 12 '14 at 15:54
  • I have looked for duplicates and things of the code iv added, its only running that code once – haydenbarton96 Nov 12 '14 at 15:58
  • I have tried the WHERE clause too, still the same. – haydenbarton96 Nov 12 '14 at 15:59
  • You need to show how this code is called. Somehow it is called twice, perhaps via a redirect, or some in page resource – Steve Nov 12 '14 at 16:05
  • There's nothing wrong with your code. What you need to do is use a WHERE clause based on a column that holds a unique value which won't change, and making sure you already have a value in your "views" column. I tested this and it worked flawlessly. What I've done is set an "id" column as `varchar` holding 2 rows; one for "john" and one for "bob", then doing `$updateviews = $conn->prepare("UPDATE pages SET views = views + 1 WHERE id = 'bob'");` or `WHERE id = 'john'` - That is your "answer". – Funk Forty Niner Nov 12 '14 at 16:25
  • I have an index.php file calling the database connect, the html theme, then on the index.php is the data to go into the theme for that page and this code to add 1 to the view counter for that page, this code is not anywhere else – haydenbarton96 Nov 12 '14 at 16:25
  • @haydenbarton96 without seeing the code, we cant help further. You might try to log the current time to a text file directly after the database code, and then check the log to see if there is indeed two calls to the code being made – Steve Nov 12 '14 at 16:34
  • I've told you what to do already. I can't help you anymore than I already have. Last ditch effort; your "type" column must be `varchar` and hold an existing value called "home". Good luck and check for errors. – Funk Forty Niner Nov 12 '14 at 16:34
  • The rest of the code is a big project (im building a cms) which is going flawlessly.. except this view counter which is only added to the one page so far to get it working then i can add to the others. However how would i log and see fi the code is being processed twice? – haydenbarton96 Nov 12 '14 at 16:37
  • Major "duh" on my part! ok it's been a long day lol! Sorry, I misread the question. I've been looking at a screen for too long and it's playing tricks on my mind. Ok, well try to see if you're doing any includes or required files, then that could explain the extra count. I know there is a function that checks if a function's been loaded more than once, but how to check if "code" has been process twice, that I don't know. An included and/or required file could be the reason why. Sometimes an include/require "once" works. – Funk Forty Niner Nov 12 '14 at 16:57
  • Haha no worries, same with me :) And there are includes (DB connection & theme), but the actual view counter code is on the main index.php file which is including the other files (DB and Theme). I have tried printing the query and its only showing it being processed once. – haydenbarton96 Nov 12 '14 at 17:12
  • directly after database call: `file_put_contents('log.txt', time() . PHP_EOL, FILE_APPEND | LOCK_EX);` then run the code once and take a look in the log.txt file – Steve Nov 12 '14 at 17:36
  • All i get in the log from running it once is this: 1415815573 1415815573 – haydenbarton96 Nov 12 '14 at 18:07
  • Try and run seperate files at a time. If you see single increments for all those, then you'll know that's not where the problem is. Then keep loading different files till you see the double increment. At this point, I'm baffled. – Funk Forty Niner Nov 12 '14 at 18:37
  • @haydenbarton96 That proves it. The log file has **two** entries, so the code is running twice, as i expected. If you need help finding out why its running twice, you will need to post more code. For the record my money is on redirect of some sort – Steve Nov 12 '14 at 23:55

1 Answers1

0

Iv fixed it, im not sure what iv done but just playing with code over time and i checked it again today and now it works.. strange.