Jump to content

Error in code?


OriginalSunny

Recommended Posts

Hi, i am getting the following error when inserting data into one of my tables using php. From what i can see the error is telling me i am inserting the same data twice for a column.

[i]sql_item: Duplicate entry '0' for key 1[/i]

I can insert data into the table once, however when run again this error comes up. If i delete the data in the table and start again, the same thing happens. The first time it will enter the data in the table and the next time the error comes up. As it is a shopping basket i need to be able to enter the data in the table more than once. Here is the code related to the problem:

$sql_ord = "INSERT INTO Order_Item
(orderID,modelnum,
quantity,price) VALUES
($orderID,
{$items_new[$i]['modelnum']},
{$items_new[$i]['quantity']},
{$items_new[$i]['price']})";
$result = mysql_query($sql_ord,$connect)
or die("sql_ord: ".mysql_error($connect));

The table it is inserting data has the following columns:

stockID - Primary Key (int (5))
orderID - int (6)
quantity - int (6)
price - decimal (9,2)

From what i remember i don't think any of the columns were auto_increment.
Thanks.
Link to comment
Share on other sites

Hey there,

$orderID seems to have a certain key to it. (unique, or primary) that doesn't allow identical values. This is good. You need one of these in every DB.

it's probably wise to use auto_increment because, when you insert a new one, the $orderID value is still at '0'.

This will cause your next insert to malfunction, because there is already a '0'.

So either change it to auto_increment or use proper orderID's. That are non-identical.

Good luck

Rogier
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.