开发者

Why mysql insert 10 rows without for loop?

I want something like this, when user press f5 to refresh the webpage, the webpage will compare current time to previous time that stored at session. If current time is 5 seconds away from previous time, then mysql will store data into database. When user refresh the page after 50 seconds, mysql should store 1 data to database, but why mysql store 10 datas to database? The user just refresh the page for 1 time, but mysql inse开发者_开发知识库rt 10 times. Why this happen? My codes :

<?php
if (strtotime($_SESSION[servertime]) < time()-5){ //10800 = 3 hours 3600 = 1 hour   
  $_SESSION['servertime'] = $servertime; 
  $_SESSION['ipaddress'] = $currentipAddress;
  $query = "INSERT INTO traceuser 
              (ibrowser, operatingsystem, ipaddress, datetime, username) 
            VALUES (
             '{$userbrowser}', '{$CurrOS}', '{$currentipAddress}'
             , '{$servertime}', '{$username}')";
  $result = mysql_query($query, $connection);
} else {
  echo "<script type='text/javascript'>alert('I did not save your info this time, because each visitor can save info for 1 time per 5 seconds.')</script>";
}
?>

I think the problem is at this line : if (strtotime($_SESSION[servertime]) < time()-5), when user fresh the page after 50 seconds, mysql will store the data to database for each 5 seconds because 50 seconds - 5 seconds = 10 seconds, so mysql insert 10 rows. How to make mysql insert only 1 row?

Update : Found the problem!!! I check my database records every 10 seconds, and I realize that it keep inserting new data for every 10 seconds, eventhough I didn't pres f5 to refresh the page. The problem is because I added Yahoo Ping Box to my website!!! From what I know, Yahoo Ping Box messenger will check new message for every 10 seconds, I am not sure how can Yahoo Ping Box code affect my php/mysql code? May I know how to prevent Yahoo Ping Box code affect my php/mysql code? This is the codes of Yahoo Ping box which cause the problem :

<object id="pingboxh57fsuytam000" type="application/x-shockwave-flash" data="http://wgweb.msg.yahoo.com/badge/Pingbox.swf" width="610" height="320"><param name="movie" value="http://wgweb.msg.yahoo.com/badge/Pingbox.swf" /><param name="allowScriptAccess" value="always" /><param name="flashvars" value="wid=oCqiyuy8QmXM8PXSd0uhP6Au" /></object>

You can test it by copy paste the Yahoo Ping Box code into your php/mysql script, you will see the magic keep inserting records into you database for every 5 to 10 seconds. May I know how to prevent Yahoo Ping Box code affect my php/mysql code?


Does your page use ajax, or some other technology which might be hitting your server multiple times per page load? I agree that the code as written would only be writing once per call. A way to help figure out what's going on would be to put a breakpoint inside your if clause, or add an error_log() there (if you don't have a debugger set up). I suspect you'll find that your code is being called multiple times by the client. Mike

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜