tags:

views:

114

answers:

4

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.

+1  A: 

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?

Kaleb Brasee
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.
RedElement
It's a long shot, but maybe there is a trigger somewhere doing an INSERT?
Andy West
I added a unique column to the database. It's still trying to run twice. This at least keeps it from adding the duplicate.
RedElement
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
@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.
Andy West
I think Andy West was referring to an SQL trigger in the DB (if MySQL supports them -- I have no idea). Also, despite your call to `die()` did you try adding logging to a file or anywhere that could confirm when and how many times this piece of code is called?
Christopher
@Christopher: To answer your question, yes, MySQL supports triggers. http://dev.mysql.com/doc/refman/5.0/en/triggers.html And you are correct about what I was referring to.
Andy West
@RedElement, logging is different than printing a message to the browser. You won't detect two requests without logging.
rick
A: 

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.

Bork Blatt
This is the only table right now. As for the code the only thing I didn't include is the mysql connection stuff.
RedElement
+1  A: 

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.

Jim Garrison
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.
RedElement
A: 

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.

rick
...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.
rick