0

I have never encountered an error like this before. I was using prepared statements when it stared and have tried everything to correct the problem including stripping the form down to it's bare components.

The following code, when executed, is creating a duplicate row.

    $sql = "INSERT INTO inventory 
    VALUES  ('','$stocknum','$year','$make','$model','$price','$body','$mile','$engine','$trans',
    '$drive','$doors','$fuel','$vin','$des','$featured','$sale','$features','$safety','$airbags','$power',
    '$exterior','$interior','','','','','')";
$insert = mysql_query($sql,$connection) or die(mysql_error());
$name = mysql_insert_id();

I can't wrap my head around why it would do this.

3
  • 1
    Is it possible that the submit page is being hit twice? Check the webserver log (or use error_log() or just send yourself an email if you don't have access to the logging facility) Commented Jan 7, 2010 at 19:15
  • @Narven would you mind telling us what was the problem in your case please ? Commented Aug 26, 2013 at 19:58
  • I was facing the same problem - it took me several hours and I lost some hair ... In my case it was in Chrome 29 with Firebug Lite activated - I guess it's something to do with a new Chrome Version, as until recently all was running smooth ... Disabled Firebug Lite and problem was gone. Commented Sep 26, 2013 at 16:20

6 Answers 6

2

I had the same problem in a project while using and orm library to access to the database. Also tried tested with mysql directly

After almost one day testing in multiple browsers and getting diferente results, i've found out that the extensions that i used (Webug) for Chrome caused tha recall to the page. After disabling the extension, it worked

I've tested some extensions that caused that problem... In chrome: Webug.

Hope it helps

Sign up to request clarification or add additional context in comments.

1 Comment

I know this is old but this turned out to be the issue.
1

The insert statement is possibly getting called twice. Did you add logging to make sure this code is only running once? And did you search to make sure there's no other code to add inventory records anywhere else?

8 Comments

It's the only code other than the posted values on the page. I thought it may be looping for some reason I wasn't aware of so I place a die() at the end but it still does it.
It's a long shot, but maybe there is a trigger somewhere doing an INSERT?
I added a unique column to the database. It's still trying to run twice. This at least keeps it from adding the duplicate.
As for the triggered insert, I've removed all possibility of that. This has been stripped down to a form with no formatting and those lines of code. Even if I only try to add to one column it still does it.
@RedElement: Just to be sure you realize, trigger code is stored in your database, not your form. Stripping down your PHP will not keep triggers from firing. That said, I don't have any reason to believe it's being caused by a trigger - just guessing at a possibility.
|
1

How many columns are in the inventory table? Is the second row an exact duplicate of the first? I don't know PHP's DB interface but I could envision a bug where, if you give it more fields than there are columns, it attempts to create multiple rows.

EDIT: A little research on the MySQL documentation finds:

INSERT statements that use VALUES syntax can insert multiple rows. To do this, include multiple lists of column values, each enclosed within parentheses and separated by commas. Example:

INSERT INTO tbl_name (a,b,c) VALUES(1,2,3),(4,5,6),(7,8,9);

The values list for each row must be enclosed within parentheses. The following statement is illegal because the number of values in the list does not match the number of column names:

INSERT INTO tbl_name (a,b,c) VALUES(1,2,3,4,5,6,7,8,9);

Depending on the contents of your variables and how the PHP/MySQL driver handles those variables (direct text substition or ? placeholders) the statement being executed may not look like you expect. Try displaying the value of $sql before you execute it.

1 Comment

I've checked to ensure the number of values inserted match the column count. Same error. I think it has something to do with my web host as now every script that adds SQL data is doing the same thing.
0

When you query the table afterwards, are you only selecting from this table or are you perhaps joining it to another table? If you have more than one child record in the joined table you will get multiple results.

Your code looks correct to me, unless there is something in the rest of the page I'm not seeing.

1 Comment

This is the only table right now. As for the code the only thing I didn't include is the mysql connection stuff.
0

In the absence of any real clear conclusion on this my observation is that I think Red Element is on the right track when he states 'I think it has something to do with my web host as now every script that adds SQL data is doing the same thing'.

I had the problem outlined above and could not see what on earth caused this until I ran the same code on a different platform and it worked fine. I was originally testing in a localhost WAMP configuration but when I promoted the code to a real server, it worked no problem.

Therefore I suggest that if anyone else has the problem it is worth a try on a different server config.

Comments

-1

I'd guess you have a problem with an .htaccess file which is making 2 requests to the same script. Log a message with a timestamp when you do your insert.

1 Comment

...and I was down-voted because? It's obvious there are 2 requests as opposed to 1 request executing the sql twice. We know this because the debug message prints once. A faulty .htaccess file can cause 2 requests. This happened to someone on another forum the other day.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.