Stats Problem

This is more of a general wondering more than anything, although might come in handy on a project I’ll be working on soon.

I have built in the past simple scripts that record stats on page views, or how many times a link has been clicked - for example:

(Pseudo Code)


// Visitor lands on page
// DB Query to get how many visits this page has had in the past
$result = $db->query("SELECT visits FROM page WHERE pageid = '1'");
$visits = $result['visits']+1;
// Update the visitor count in the database by 1
$db->query("UPDATE page SET visits = '$vists' WHERE pageid = '1'");

Now, whenever I have done this (as I’ve said I’ve done similar that updates every time a link is clicked, or a vote is made for example) sometimes 1 visit actually increments by more than 1. So I’ve ended up with stats showing how many times certain links have been clicked on a site, and they are ludicrously high - or how many votes an item on a website has had and again its stupidly high.

Is there something I’m missing? Obviously if the page is loaded over and over again by refreshing or someone repeatedly clicking the link it will cause erroneous results, but I’ve had people in the same room click a link once and I watch the DB increment by 10 or 20.

It’s probably something quite simple or some kind of validation that will make the results more accurate, but I thought I’d post as it seems very weird to me!

I can’t think of anything, but you’re over complicating things, this one-liner would do. :slight_smile:


<?php
$db->query('UPDATE page SET visits = visits + 1 WHERE id = 1;');

Unless of course you add any other sort of validation, filtering etc.

True, depends on the script really, the example was just a general example of the kind of script I was referring to. I’m guessing that its unlikely to be the script itself though more likely some kind of multiple loading issue or something, so only included a basic example.