0

I'm making a visit counter for a page, and using the following code in its own php file, which is only being called once. However, the increment is increasing by 5 each time. I've examined the page many times, and confirmed that the snippet is only being called once. There must be some reason that the increment is increasing by 5, rather than one, I'm missing something, what are the possibilities?

<?php
// property visit counter
include("admin/includes/db_open.php");
function increaseViewCount($propID,$currentCount){
  global $MyDB;
  $MySQL = "SELECT DISTINCT prop_visitCount FROM ap_property WHERE property_ID = ". $propID;
  $MyQuery = mysql_query($MySQL,$MyDB) or die(mysql_error());
  $MyRowCount = mysql_num_rows($MyQuery);
  //if ($MyRowCount > 0){
    $this_count = $currentCount;
    $this_count++;
    $UpdatePropCount = "UPDATE ap_property SET prop_visitCount = ". $this_count . " WHERE property_ID = " . $propID;
    $PropCountQuery = mysql_query($UpdatePropCount,$MyDB) or die(mysql_error());
  //}
  mysql_free_result($MyQuery);
  return $this_count;
}
$count = increaseViewCount($_GET['property_ID'],$property_VisitCount[0]);
include("admin/includes/db_close.php");
?>
  • 3
    Please, [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and use [PDO](http://us1.php.net/pdo). – Jay Blanchard Apr 15 '15 at 16:55
  • Using `$_GET['property_ID']` and string concatenation to generate queries is the simplest recipe for disaster (it's called [SQL injection](https://en.wikipedia.org/wiki/SQL_injection)). Incrementing the value in PHP instead of using `UPDATE ap_property SET prop_visitCount = prop_visitCount + 1` produces incorrect values when there are many simultaneous requests. – axiac Apr 15 '15 at 17:28

1 Answers1

0

the query looks fine, but instead of include use include_once to include this file. you probably include some file a few times somewhere.

Alex
  • 5,759
  • 1
  • 32
  • 47
  • This is partly why this is so perplexing - I've tried include_once and require_once, but the increment is still 5. – common-ground Apr 15 '15 at 17:26
  • write something to the log, add this to your function: `error_log( __FUNCTION__ )` it will at least tell you how often the function is called. – Alex Apr 15 '15 at 17:51
  • I've just now noticed that the problem presents on Chrome, but not Firefox or Safari. – common-ground Apr 15 '15 at 22:23