PHP Insert Twice into MySQL Database on Single Request
The code snippet below performs operations to insert data into a MySQL database. However, it inserts the results twice, even after running only once. This issue only arises when Opera or Chrome is used as the browser.
$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 />"; }
Solution:
To resolve this issue, ensure that the insert query is only executed for a specific request. This prevents it from running multiple times for the same request.
In the .htaccess file, modify the code to point everything to the index file. This ensures that the script is only requested once. Subsequently, the insert query will only be executed once, preventing duplicate insertions.
The above is the detailed content of Why Does My PHP Code Insert Data Twice into MySQL on a Single Request with Opera or Chrome?. For more information, please follow other related articles on the PHP Chinese website!