MySQL Insert Saving record twice

A

Anonymous

Guest
Hope someone has run across this - I cannot seem to find the solution. I am not certain whether the problem is in code or in the database. I have run into others with the same problem in a couple of other forums, but no one seems to have found an answer.

I have a simple self-contained form (posting to itself). When I insert a new record in the database, it is saving it twice. A few times, rather than saving the same, new data, twice, it saved the new data in one record and duplicated another already existing record, which, of course, make me tend to think it is a DB problem. However, I have set up the same database from scratch on two different servers with two different versions of MySQL, and had the same results.

The form is very simple - in a nut shell:
Code:
<?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>

That's it! Any comments would be appreciated.
 
when it is happening?
i mean,
when you are clicking submit, then it is happening or
after submitting the form and if you click refresh the page... then it is happening..

because i feel that may be you are refreshing the page
 
Yes - my first though was that it must be the code. However, I took the code down to bare bones, cleared the post variables in the next line after the insert and directly exited the script, none of which changed the results.

After adding two fields to the database, making one of the new fields a primary key (forgetting to remove the primary key from the ID field), and removing the auto-increment from the ID field, I was able to save two records without duplication.

I then removed the primary key from the new field, set auto-increment back on in the ID field (so now the db is back to original except for two additional fields) and the problem has stopped.

I am still puzzled as to the nature of the problem. Fortunately, I have an identical test database which was producing the same error, so I can do some better testing to find out the nature of the problem.

I won't be able to get to it right away, but when I do, I will post my results.
 
Back
Top