gardouth Posted April 23, 2009 Share Posted April 23, 2009 Hi, hope someone can help. I am a reasonably experienced user of PHP with MySQL database and can solve most problems I come up to however this one I just can't get an answer to. The problem is that I have a basic form that users can use to add data to a MySQL database. Its a simple setup; the form POST's to a PHP script which performs an INSERT INTO the MySQL database. I've done this hundreds of time on many sites with no particular problems and I'm fairly sure that I'm doing right. However sometimes (randomly and inconstantly) when the MySQL database is updated, two new rows are added with the same data. I really can't see any reason for it. I have tried all sorts to fix it including disabling the submit button so it can only be clicked once but that had no effect. The only solution I have come to that works all the time is to have an additional column in the database for a unique id (and make it a unique value column) then submit a unique id as a hidden value from the form. This seems a very messy way to solve something that shouldn't happen in the first place! I also should point out that this happens on several sites I have built with different servers and different MySQL databases, not just one. Interestingly I had the same problem some time ago when using an Access database with ASP but put it down to Access being rubbish! Hope someone can help. Link to comment https://forums.phpfreaks.com/topic/155326-mysql-duplications/ Share on other sites More sharing options...
Mchl Posted April 23, 2009 Share Posted April 23, 2009 Perhaps show us the part of code that's responsible for inserts? Link to comment https://forums.phpfreaks.com/topic/155326-mysql-duplications/#findComment-817175 Share on other sites More sharing options...
gardouth Posted April 23, 2009 Author Share Posted April 23, 2009 Sorry, that would help of course. Below is the code that gets the form data then submits it to the database. I have tried using $_POST as well as $_REQUEST $val1 = $_REQUEST["val1"]; $val2 = $_REQUEST["val2"]; $val3 = $_REQUEST["val3"]; $val4 = $_REQUEST["val4"]; mysql_query("INSERT INTO table1 (Field1, Field2, Field3, Field4) VALUES ('$val1', '$val2', '$val3', '$val4')"); header("Location: index.php"); Link to comment https://forums.phpfreaks.com/topic/155326-mysql-duplications/#findComment-817195 Share on other sites More sharing options...
PFMaBiSmAd Posted April 23, 2009 Share Posted April 23, 2009 Your browser is probably requesting the page twice. Different browsers do this for different reasons and URL rewriting can also cause it. Assuming there is not something in the part of your code you did not post that is causing it, the best solution is to set a session variable in your form processing code at the point that you have successfully processed the submission and to also check at that start of the form processing code if this session variable has already been set and to skip processing all submissions after the first one. Link to comment https://forums.phpfreaks.com/topic/155326-mysql-duplications/#findComment-817299 Share on other sites More sharing options...
revraz Posted April 23, 2009 Share Posted April 23, 2009 Check your code for a possible second Query that is inserting it again. Link to comment https://forums.phpfreaks.com/topic/155326-mysql-duplications/#findComment-817303 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.