I've got a simple code below. After it's run once, it inserts results twice into the mysql database.
if it run twice or request twice base on 1 refresh on the page, why the output is just 1 result?
I have been googling for the whole day and struggling to resolve this issue. However, I failed to figure out what is wrong with this code. The code runs perfectly on localhost, but after it's moved to the server, the problem pops up. Has anyone faced something like this before? How can this problem be resolved?
FULL CODE:
<?php
$db=mysql_connect('localhost','zzzzzzz','xxxxxx') or die('Unable to connect.'.mysql_error());
mysql_select_db('test',$db) or die(mysql_error($db));
$sql="INSERT INTO test_table(value,insert_time) VALUES ('testing','".time()."')";
$result=mysql_query($sql);
echo "result=".$result;
$select="select * from test_table";
$rs=mysql_query($select);
while($row=mysql_fetch_array($rs)){
echo $row["test_id"]." -- ".$row["value"]." -- ".$row["insert_time"]."<br />";
}
?>
RESULT:
result=1
1 -- testing -- 1298185509
BUT IN DATABASE:
test_id , value , insert_time
1 , testing , 1298185509
2 , testing , 1298185511
Do you see this
And the next line:
This means that you run your query twice. Remove one of the
$db->query
, e.g.:This problem may also be arising due to you may be mixing the
GET
andPOST
Or
you may have downloaded online template which is may be making an background ajax call before the page is submitted.
Please have a look and if you find a solution or if there is any other problem let us know.
I guess this code from index.php which is front-controller for all requests.
When browser fetches page it usually trying to get favicon.ico too. And if favicon is missing, second request is processed your php file again. You can examine this supposition by writing $_SESSION['REQUEST_URI'] to DB.
I recommend do not change data in GET requests and research web server access log from time to time.
As you mentioned the problem does not occur when you test locally, only occurs when you deploy in server --- I guess you have Google Adsense in your page. In that case, adsense crawler is crawling your page (sending the second http request to the url)
To avoid this, add following in start of your php file:
I had faced similar issue in a script which sends emails. I was getting double emails per action :(
Then, I investigated into the server access log, and discovered the second request per action from this user agent ...
There're many reasons to cause this, so I would suggest you using the best practice:
If you're creating a new record in your DB, you should init the logic with a POST request.
The non-code problems should be easily avoided now: .htaccess thing, Firebug thing, ads tracking thing...
I had the same issue on my site. After debugging I found the root cause. It is Yandex Metrica js library. It calls ajax request to the same page to calculate loading time and some other params.
Do you use it? If not open chrome dev panel and look at http request. May be it is request for favicon or etc.