Page 1 of 1

Duplicate INSERTS

Posted: Mon Sep 13, 2010 11:18 pm
by ama
I have had a random problem for 3 months now. I have read literally dozens of forums and have found various answers but no solutions. I have a php script that includes an insert sql statement that I use with mysql_query. When I run the script It will insert the line initially, but within 3-4 hours the same record is inserted 2 more times making 3 copies total. I have run similar scripts on 2 different servers with the same results. I thought that it was only happening on Mozilla and not on Chrome or IE, but now, I have duplicated it with IE.

I have read people who have had this problem with Mozilla and it had to do with Header information. I have explored that without success. I've heard of similar behavior if you have an img tag where src=="", or if you have an error in a css file, then firefox will load the page twice. I don't believe that is my problem as it happens with IE.

I also tested it with a VERY simple script (which I am including). I'm not sure if it is a browser thing, a coding thing, a header thing, a syntax thing. I am at a loss. Please help.

This is my whole script:

Code: Select all

<?php
    $mydb = mysql_connect("MY_SERVER", "MY_UN", "MY_PW") or die(mysql_error());
    mysql_select_db("MY_DB") or die(mysql_error());

    $query = "INSERT INTO players (ID, first, last, pos, birth, rookie) VALUES ('Test', 'Joe', 'Practice', 'QB', 1990, 1990)";
    mysql_query($query) or die(mysql_error());
    echo("done");

?>
Thanks, Alan

Re: Duplicate INSERTS

Posted: Tue Sep 14, 2010 1:33 am
by Gargoyle
first off, your data normalization is flawed if copies of the same entry exist while you do not want them. if you fix this, it will eliminate the problem even if you don't find the source.

second, you really need to find out *when* exactly this happens. some weeks I had a roughly similar problem that was driving me insane. one of our freelance programmers finally found out that the problem existed because our htaccess file would rewrite the url to index.php if certain images could not be found, thus creating pageviews that seemed wrong.

if it's not the rewrite engine, it could be a header redirect somewhere.

Re: Duplicate INSERTS

Posted: Tue Sep 14, 2010 5:18 am
by jazzercising011
hmm one quick way to fix this without too much research is to have the code compare with the database. If a duplicate entry exists then throw an error message like "duplicate entry exists".

Re: Duplicate INSERTS

Posted: Tue Sep 14, 2010 6:25 am
by stuartr
I would agree with the comment re database normalisation. Although you are using a primary ID key, you need to consider whether you should also have a unique key based on some or all of the fields.

That aside, have you been through your web server and mysql logs to see if anything if being recorded there to help identify what is going on? As Gargoyle also says, it could well be a rewrite engine or a header redirect which may well show up in the logs.

Re: Duplicate INSERTS

Posted: Tue Sep 14, 2010 7:41 am
by PradeepKr
May be a back button resubmits the form again.

Re: Duplicate INSERTS

Posted: Tue Sep 14, 2010 11:54 pm
by ama
By posting my question, Murphy's law came to my rescue. After trying to find the solution for 3 months, I finally found the answer the day after posting to the forum. Much thanks for the suggestion to check the logs. I had to dig around but finally found logs on my server which pointed me to the problem

For me, my css file was borrowed from another project I am working on, so naturally there are things that don't apply to this project, but no big deal right? Well, the css file referenced a background image for a banner. That background image wasn't in the folder with the new main.css file so when I tried to access the page it would open the css file, then try to find the background image. When it was unable http threw a 404 then tried again, resubmitting the form. When I commented out that line, everything worked fine.

I hope I can save someone 3 months of their lives by finding this post.
Alan