1

I have been stuck on this for a while and can't figure it out.

I use this code to record visits and everytime the page is loaded once it inserts two rows, 1 unique and 1 non unique, how is that?

$check = mysql_query("SELECT ip FROM visits WHERE ip = '{$_SERVER['REMOTE_ADDR']}'");
$num= mysql_num_rows($check);
if ($num< 1){
mysql_query("INSERT INTO visits VALUES('','{$_SERVER['REMOTE_ADDR']}','1')");
}else{
mysql_query("INSERT INTO visits VALUES('','{$_SERVER['REMOTE_ADDR']}','0')");}

I know its not the best script to count visits because of ip changes and such but thats a different story, just wondering why this inserts twice on a page load

When i move the code to a separate file without any HTML to test it with fake ip and such it works, what kind of html on the page could cause this?

  • It may be unrelated to the question, but you should have a look at this: http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php – aborted Feb 17 '13 at 11:59
  • Yeah i have other websites where i use mysqli with prepared statements, i just havent migrated this one yet, i doubt that is the problem though. – Brendan Mullan Feb 17 '13 at 12:01
  • Unlike you expect, the page is probably not loaded once. Is there some kind of redirect, or is this code called from multiple places? Is this code part of an include which is included more than once? – mvds Feb 17 '13 at 12:04
  • No apart from the db.php include there is no others or any redirects at all. – Brendan Mullan Feb 17 '13 at 12:06
  • Begin debugging by placing a `var_dump('got here');` at various places, beginning at the top, and working your way down through the block of code. By doing this, you may find that 'got here' appears twice at some point. Then, you'll need to determine why. – Gor Feb 17 '13 at 12:14
  • mail yourself by using php's `mail();` function. If you get 2 emails, then it's executing twice. – Gor Feb 17 '13 at 12:23
  • Maybe not related, but do you have plugins installed in your browser (like firebug) that may be loading the page on their own? I've run into something like that once and disabling all plugins fixed if for me. – thaJeztah Feb 17 '13 at 12:43

1 Answers1

1

May be not an answer to your question, but I think you need to optimize your statements. Otherwise you can have like thousands of records with repeating ips and 0 or 1 times visited.

  • IP address - varchar -(UNIQUE KEY)
  • Times visited. - mediumint -

Then you could make it easier (UPDATED):

$ip = $mysqli->real_escape_string($_SERVER['REMOTE_ADDR']) ;
$update = "INSERT INTO visits (ip, times_visited) VALUES ('{$ip}', 1)
ON DUPLICATE KEY UPDATE times_visited = times_visited+1 ; " ;
sybear
  • 7,837
  • 1
  • 22
  • 38
  • That looks good, would that still insert a seperate row for each non unique if they were to reload, so i keep track of that too? – Brendan Mullan Feb 17 '13 at 12:25
  • If an IP does not exist in your database, you will add it and set times_visited to 1. Else if IP already exists you just increment times_visited by 1. So you won't have even 2 rows with the same IP. Just saying that you could do it all in just one query. – sybear Feb 17 '13 at 12:29
  • Yeah that looks good, the query is giving me an error though 'right syntax to use near 'SET times_visited = times_visited+1' it seems fine too me not sure why it would error – Brendan Mullan Feb 17 '13 at 12:38
  • Could you post all your mysql statement here? – sybear Feb 17 '13 at 12:40
  • I created a new table to test it before i can put it live with the ip unique and time visited, and used exactly your query but it gives that error – Brendan Mullan Feb 17 '13 at 12:43
  • Ok I figured out. Check the edit. Now it should work, hope it helps. – sybear Feb 17 '13 at 12:50
  • That works great thank you, it still inserts twice by setting times visited too 2 on the first page load but its better than record after record – Brendan Mullan Feb 17 '13 at 12:55