Jump to content

Saving Data twice


rationalrabbit

Recommended Posts

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!

Link to comment
Share on other sites

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>

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.