php运行一次并在mysql数据库中插入两次 [英] php run once and insert twice in mysql database

查看:106
本文介绍了php运行一次并在mysql数据库中插入两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面有一个简单的代码.它运行一次后,会将结果两次插入mysql数据库.

I've got a simple code below. After it's run once, it inserts results twice into the mysql database.

如果它运行两次或基于页面上的1次刷新请求两次,为什么输出仅为1个结果?

if it run twice or request twice base on 1 refresh on the page, why the output is just 1 result?

我整天都在搜寻Google,并努力解决此问题.但是,我无法弄清楚这段代码出了什么问题.该代码可以在localhost上完美运行,但是将其移至服务器后,问题就会弹出.有人遇到过这样的事情吗?该问题如何解决?

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 />";
}
?>

结果 :
结果= 1
1-测试-1298185509

RESULT:
result=1
1 -- testing -- 1298185509

插入数据库:
test_id,value,insert_time
1,测试,1298185509
2,测试,1298185511

BUT IN DATABASE:
test_id , value , insert_time
1 , testing , 1298185509
2 , testing , 1298185511

推荐答案

我正面临与您相同的问题,仅当我使用Opera或chrome时才出现此问题.

I'm facing the same issue as you, the problem only occus when I use Opera or chrome.

对于我来说,我有一个.htaccess,将所有内容都指向索引文件.自然,浏览器会两次请求该脚本,一次是针对自己的脚本,另一次是针对图标的访问.

For my case, I have an .htaccess to point every thing to the index file. Naturally the browser will request the script twice, once for the script it self, the other is for the favicon.

修复: 当浏览器请求favicon.ico

这篇关于php运行一次并在mysql数据库中插入两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆