php在mysql数据库中运行一次并插入两次

2022-01-09 00:00:00 insert 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?

我整天都在谷歌上搜索并努力解决这个问题.但是,我无法弄清楚这段代码有什么问题.代码在本地主机上运行完美,但是移到服务器后,问题就出现了.有没有人遇到过这样的事情?如何解决这个问题?

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?

完整代码:

<?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.

解决方法:尝试编辑 .htaccess 以防止在浏览器请求 favicon.ico

The fix: Try to edit the .htaccess to prevent redirection to the index file when the browser is requesting for favicon.ico

相关文章