rationalrabbit Posted August 8, 2007 Share Posted August 8, 2007 PHP 5, MySQL 5 I have a small form that is posting to itself by setting a variable which is read at post time to detect the submit. (if $_POST['variable']) I have several other forms very similar that work fine (except that they use $_SERVER['PHP_SELF']; in the form action, where this one uses the file name). For some reason, this form is entering two records with each save to the database. I've gone through the flow, put an exit right after the save, cleared the post variables directly after the save - still does the same thing. Following is my save routine (this is the only thing very different from my other forms): $SaveIt = "INSERT INTO CheckList (NameFirst, NameMid, NameLast, Position, StartDate, Salary, Hourly, Status, App, LCV,DirDep, EmpBook, W4, I9, SSCard) VALUES ('$NameFirst', '$NameMid', '$NameLast', '$Position', '$StartDate', '$Salary', '$Hourly', '$Status', '$App', '$LCV','$DirDep', '$EmpBook', '$W4', '$I9', '$SSCard')"; if(mysql_query($SaveIt)) { notice that data has been saves } else { error message } Any ideas would certainly be appreceiated! Quote Link to comment Share on other sites More sharing options...
rationalrabbit Posted August 9, 2007 Author Share Posted August 9, 2007 See More on this subject under "Insert doing double duty" (I did not notice this when I posted.) Quote Link to comment Share on other sites More sharing options...
teng84 Posted August 9, 2007 Share Posted August 9, 2007 ok tell us the real prob and how you got this thing Quote Link to comment Share on other sites More sharing options...
rationalrabbit Posted August 9, 2007 Author Share Posted August 9, 2007 Ah ... pardon me? This is a real problem. And what does "got this thing" mean? Did you post here to be productive or sarcastic? Further testing appears to reveal this may be a MySQL problem, as some inserts, rather than inserting the same data in two different records, inserted the data in one record and duplicated another, already existing record in the database. I have run this on two different databases on two different servers. Further research reveals that I am not the only person experiencing this problem. The form is very simple - in a nut shell: <?PHP if ($_POST['process'] == 1) { Place values from the POST variables back to regular variables and perform some simple validation Create error messages for fields that do not validate Set error variable to true if any fields do not validate if all fields validate (error variable is false) { if (!$connect = mysql_connect($HOST, $USER, $PASSWORD)) { echo ("Error: Did not connect to database!"); } else { mysql_select_db($DBName) or die ('Unable to select database!'); $SaveIt = "INSERT INTO CheckList (NameFirst, NameMid, NameLast, Position, StartDate, Salary, Hourly, Status, App, LCV,DirDep, EmpBook, W4, I9, SSCard) VALUES ('$NameFirst', '$NameMid', '$NameLast', '$Position', '$StartDate', '$Salary', '$Hourly', '$Status', '$App', '$LCV','$DirDep', '$EmpBook', '$W4', '$I9', '$SSCard')"; if(mysql_query($SaveIt)) { echo('<div style="border:2px solid #000000; background-color:#0000FF; color:#FFFFFF; padding:3px; font-weight:bold; text-align;center;"> Your Data Has Been Saved </div>'); clear($_POST); } else { echo('<div style="border:2px solid #000000; background-color:#FF0000; color:#FFFFFF; padding:3px; font-weight:bold;"> There was an error in processing the data </div>'); } } } else // nothing has been submitted yet { set variables for form } } ?> <html> Print top of page <? Print any error messages if fields did not validate ?> <form name="NewEmpCheck" method="post" action="newemployee.php"> // This form, of course input fields, ending with: <input type="hidden" name="process" value="1"></input> <input type="submit" name="Submit" value="Submit"></input> <input type="reset" name="reset" value="Reset"></input> </form> </html> Quote Link to comment Share on other sites More sharing options...
fenway Posted August 15, 2007 Share Posted August 15, 2007 Unless you have a trigger or something else fancy, one insert = one insert. Blame PHP. Quote Link to comment Share on other sites More sharing options...
Illusion Posted August 16, 2007 Share Posted August 16, 2007 Don't blame php too , only blame ur code. As u have not posted all ur code, I guess ur script is surely executing twice, u may have missed something like this if(isset($_POST['Submit']) { // } Quote Link to comment Share on other sites More sharing options...
rationalrabbit Posted August 23, 2007 Author Share Posted August 23, 2007 I took the code down to basic bare bones. Still had the same problem. I then removed the auto-increment from the ID field and moved the primary key to another field, and saved two entries without the duplication problem. I then re-instigated the auto-increment in the ID field, and moved the primary key back to that field. I no longer have the problem, and everything is as it was originally. ??? I still have an identical database that I have not altered. I will try to do some more careful tests with that database, as I really want to know the problem source. I'm quite swamped right now, so it will be several days before I have the time to do that, but I will post the results here. As far as isset($_POST['Submit']), I would think that $_POST['process'] == 1 should work as long as the value is reset to 0 or undefined. I did not do this, however, until after the insert code. Should not matter though, should it? In testing, I also halted the program and exited directly after the insert code, Neither of these solutions prevented the problem. The code is working fine now, using "if($_POST['process'] == 1)", so I would say that does not appear to be the problem. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.