php run once and insert twice in mysql database

2019-01-15 15:18发布

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

标签: php mysql insert
12条回答
劳资没心,怎么记你
2楼-- · 2019-01-15 15:54

Ok, I know that this doesn't make any sense, but I solved removing $.ajax({async:true}); from my js file.

查看更多
我欲成王,谁敢阻挡
3楼-- · 2019-01-15 15:54
$result=mysql_query($sql); echo "result=".$result;

It's because you run mysql_query($sql) twice. Try it without echo the $result.

查看更多
beautiful°
4楼-- · 2019-01-15 15:55

Code is fine.

Try to change browser. If still not worked.

Restart server Still not

Try to write in another file outside of project as single script.

It will work.

I tried your code its working on my system.

查看更多
▲ chillily
5楼-- · 2019-01-15 15:55

Your code seems fine, and it should be probably due to external plugins that you are using, which send a 2nd request each time.


If you are using Google Chrome, it would most likely be due to this bug, https://bugs.chromium.org/p/chromium/issues/detail?id=123121. This bug causes Chrome to redirect every request into index.php, so the PHP script will run twice.

This can be fixed using the following code, as according to website.

RewriteBase /
RewriteCond %{REQUEST_FILENAME} !favicon.ico
RewriteRule .* index.php [L]

You can also debug using debug_print_backtrace: http://php.net/debug_print_backtrace.

debug_print_backtrace() prints a PHP backtrace. It prints the function calls, included/required files and eval()ed stuff.


Alternatively, you can use SQL UNIQUE Constraint to prevent duplicate rows from being inserted in the first place. Find out more at http://www.w3schools.com/sql/sql_unique.asp.


Just a note: There are mysql() have been deprecated, use MySQLi instead.

查看更多
一纸荒年 Trace。
6楼-- · 2019-01-15 15:56

There is no problem with your code.Firebug or similar tools can make double request sometimes. Also metric scripts (yandex metrica, chartbeat etc) in your page can do same behavior. You should inspect these situations.

To debug it you can log insert requests, and you can inspect it to find what is wrong.

If you are using linux environment ;

Add a log line to your code :

error_log(print_r($_SERVER,true));

Open a console window and type (log directory depends on your sever configuration)

tail -f /var/log/apache/error.log

And then you can call your insert page. At every request you can see what is happening.

查看更多
何必那么认真
7楼-- · 2019-01-15 15:59

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

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.

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

查看更多
登录 后发表回答