tags:

views:

204

answers:

3
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?

+3  A: 

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.

Eric
+1 for using Occam's Razor in an Answer!
St. John Johnson
The table was empty before,and with two records after this statement.And there is no outer loop.
Shore
Then you're calling it twice somehow. I recommend putting an `echo` in that loop to figure out when it's being called.
Eric
+2  A: 

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.

willoller
That doesn't explain why he had two of the same `$i` being inserted.
St. John Johnson
Wait he doesn't say 2 of the same $i, just 2 records are inserted. Occam's on my side!
willoller
There's a reference to $_POST. Could the user be double clicking the submit button?
Glenn
Used your method,only to find that it got run for once only.
Shore
+1  A: 

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

johnc