Home » Performance » MySQL » Insert with PHP Save Record Twice
Insert with PHP Save Record Twice [message #1583] Fri, 10 August 2007 08:44 Go to next message
RationalRabbit  is currently offline RationalRabbit
Messages: 4
Registered: August 2007
Location: Seattle, Washington
Junior Member
MySql 4.1.15 & 5.0 PHP 4.4.4 & 5.1.6
I have a simple self-contained form (posting to itself). When I insert a new record in the database, it is saving it twice.

I have set up the same database from scratch on two different servers with two different versions of MySQL, and had the same results. I have written this code several different ways, as well as separating the HTML and the PHP processing into two separate files, all still with the same results. I have taken the code down to bare bones (4 fields with no validation, etc.) and I still have the same results.

This doesn't seem to be a known bug, and with several web searches on the subject, I have run into only a few other people with the same problem. So far, no resolves.

Here is the code 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) VALUES ('$NameFirst', '$NameMid', '$NameLast')";
            if(mysql_query($SaveIt))
            {
               echo('<div>Your Data Has Been Saved </div>');
                 clear($_POST);
            }
            else
            {
               echo('<div> 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> 

Another way I have done this with the same results:
         mysql_select_db($DBName) or die ('Unable to select database!');
         $xQuery = "INSERT INTO CheckList set NameFirst = '$NameFirst', NameMid = '$NameMid', NameLast = '$NameLast', Position = '$Position'";
         $SaveIt = mysql_query($xQuery);
         if(!$SaveIt)
         {
            echo('<div>There was an error in processing the data </div>');
         }
         else
         {
            echo('<div> Your Data Has Been Saved</div>');
              clear($_POST);
         }


I'm really puzzled - especially since I have similar forms that work fine. I'm no PHP/MySQL guru, by any means. I have spent hours working on this code, and have pretty much come to conclusion that the error is not in the code - there must be something in how I set up the database.

The structure of the database is:
http://www.filerabbit.com/images/structure.jpg
With the following indexes:
http://www.filerabbit.com/images/structure2.jpg
Re: Insert with PHP Save Record Twice [message #1589 is a reply to message #1583 ] Thu, 16 August 2007 06:36 Go to previous messageGo to next message
Peter  is currently offline Peter
Messages: 405
Registered: August 2006
Senior Member
Super Guru
Enable MySQL log and see which queries your application actually send and if it is single connection or different connections.

Also looking in web server logs may help - may be for some reason you get 2 web requests instead of one.


Peter Zaitsev, MySQL Performance Expert
MySQL Performance Blog - http://www.mysqlperformanceblog.com
MySQL Consulting http://www.mysqlperformanceblog.com/mysql-consulting/
Re: Insert with PHP Save Record Twice [message #1677 is a reply to message #1589 ] Thu, 23 August 2007 04:53 Go to previous messageGo to next message
RationalRabbit  is currently offline RationalRabbit
Messages: 4
Registered: August 2007
Location: Seattle, Washington
Junior Member
After removing the auto-increment from the ID field, and moving the primary key to a different field, I was able to save two entries with no duplicates.
I then re-instigated the auto-increment on the ID field, and moved the primary key back to that field.
After doing the above, the problem went away.
I still have another database that I have not altered. I will do a more careful study of the problem with that database, and see if I can nail it down. When I do, I will post the results here. May be a number of days, though, as I am quite busy.

Thanks for your comments, Peter. I didn't think of checking logs.
Re: Insert with PHP Save Record Twice [message #2040 is a reply to message #1583 ] Tue, 09 October 2007 11:15 Go to previous messageGo to next message
hshi  is currently offline hshi
Messages: 2
Registered: October 2007
Junior Member
hi, it seems to me that you just executed this query twice.

$SaveIt = mysql_query($xQuery);
if(!$SaveIt)

Cheers

Hui
Re: Insert with PHP Save Record Twice [message #2047 is a reply to message #1583 ] Wed, 10 October 2007 05:56 Go to previous messageGo to next message
RationalRabbit  is currently offline RationalRabbit
Messages: 4
Registered: August 2007
Location: Seattle, Washington
Junior Member
Certainly appears so, doesn't it! Shocked
Not even sure what I was doing there.
However, the code block above was still saving twice, and the problem went away when I made a change to the database.
I still have the original code and the original database set up on one server. I've been wanting to go back and check the whole thing out, but haven't had the time.

One of these days, I will, and post a followup.

Thanks for the observation, Hui!

[Updated on: Wed, 10 October 2007 06:07]

Re: Insert with PHP Save Record Twice [message #2048 is a reply to message #2047 ] Wed, 10 October 2007 07:38 Go to previous messageGo to next message
hshi  is currently offline hshi
Messages: 2
Registered: October 2007
Junior Member
No Sweat! I had that exact problem before. That's why I could spot it instantly. Cool

Cheers

Hui
Re: Insert with PHP Save Record Twice [message #3310 is a reply to message #2047 ] Thu, 17 July 2008 05:39 Go to previous messageGo to next message
sdkpnl  is currently offline sdkpnl
Messages: 1
Registered: July 2008
Junior Member
Hi RationalRabbit.

Did you ever find out more about the problem?
It seems like I have the same problem.
Re: Insert with PHP Save Record Twice [message #3326 is a reply to message #1583 ] Tue, 22 July 2008 14:31 Go to previous message
RationalRabbit  is currently offline RationalRabbit
Messages: 4
Registered: August 2007
Location: Seattle, Washington
Junior Member
Unfortunately, no. And never had the problem again. But I still have the code and will take a look at it. Unfortunately, that may take a few days to get to, though. If you find the answer, would certainly appreciate knowing about it. If not, and I have not replied to this by the 28th, feel free to prompt me.

There was something odd about this. I still don't think it was in the code, but I'll need to go back and refresh my memory.

[Updated on: Tue, 22 July 2008 14:34]

Previous Topic:Triggers
Next Topic:How to optimize this query?
Goto Forum:
  



Current Time: Thu Jan 8 18:29:32 EST 2009

Total time taken to generate the page: 0.02682 seconds
.:: Contact :: Home :: MySQL Support by Percona.com ::.

Powered by: FUDforum 2.7.5.
Copyright ©2001-2006 FUD Forum Bulletin Board Software

MySQL Performance | Forum authority Badge