omg
omg

Reputation: 140112

Insert once, but two records created in MySQL

for($i = 1 ; $i <= 3; $i++)
{
    if(!empty($_POST['fl' . $i]))
    {
        $dml = "insert into flAptitude(accountId,language,qualification,certificate) value($accountId,'" . $_POST['fl' . $i] . "','" . $_POST['qualification' . $i] . "','" . $_POST['certificate' . $i] . "')";
        mysql_query($dml,$con);
        file_put_contents("fl$i.txt",$dml);
    }
}

And there are only fl1.txt created,whose content is:

insert into flAptitude(accountId,language,qualification,certificate) value(1,'Germany','Excellent',NULL)

So,under what condition will MySQL generate 2 record for 1 "insert" statement?

EDIT Here is the table definition:

mysql> show create table flAptitude\G
*************************** 1. row ***************************
       Table: flAptitude
Create Table: CREATE TABLE `flaptitude` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `accountId` int(10) unsigned default NULL,
  `language` varchar(15) NOT NULL,
  `qualification` int(10) unsigned default NULL,
  `certificate` varchar(20) default NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

Does it have anything to do with AUTO_INCREMENT=3?What does it mean?

Upvotes: 2

Views: 4494

Answers (5)

Artur
Artur

Reputation: 33

You wrote your questions in 2009 but maybe my answer will be useful for others...

I had the same issue. In my case it was the FirePHP. Add-on to chrome/firefox. It (I don't know why) doubled my scripts. When I lunch browser without extensions everything was OK.

Upvotes: 1

Patrick
Patrick

Reputation: 1

I found this issue to be solved by putting an exit(); after my header statement;

  $sql = "INSERT INTO airline SET
            company         = '$company',
            name            = '$name',
            address1        = '$address1',
            address2        = '$address2',
            city            = '$city',
            st              = '$st',
            zip             = '$zip',
            ph1             = '$ph1',
            ph2             = '$ph2',
            fax             = '$fax',
            email           = '$email',
            web             = '$web',
            date            = '$date'";

  if (!mysql_query($sql)) {
    mysql_close();
    die("<p><b>Record Not Added </b><br />" . mysql_error() . "</p>");
  }
  else {
    mysql_close();
    $goto = "Location: $php_self";
    header($goto);
    exit();
  }

When I omitted the exit();, the code would add 2 records of exact same data, but incrementing IDs. Adding the exit made the script only write once.

Upvotes: 0

johnc
johnc

Reputation: 40233

To answer the last part of your question, no it has nothing to do with AUTO_INCREMENT, which is an attribute to auto-id new rows, in this case starting at 3

Upvotes: 1

Eric
Eric

Reputation: 95223

Occam's Razor tells me that you've probably looped through this thing twice with an outer loop. This could also occur if you had a malformed trigger on the table that was duplicating results. Aside from that, we'd need to see what the select * from flAptitude was before and after this code ran to get a better sense as to what it was doing.

AUTO_INCREMENT=3 just means that the autoincrement column starts at 3 instead of the default 1.

Also, you're using InnoDB, which is an ACID database engine. This means that you're calling your insert twice in your code, you just need to find out where.

Upvotes: 3

willoller
willoller

Reputation: 7330

This is a php issue, not a problem with MySQL. The issue is file_put_contents() overwrites the content of the file. Therefore, when your loop happens (loops twice) your log only has the last record inserted.

Try this:

$file = "fl{$i}.txt";
$current = file_get_contents($file);
$current .= $dml . "\n";
file_put_contents($file, $current);

More on file_put_contents() here.

Upvotes: 2

Related Questions