Jump to content


Photo

Error in code?


  • Please log in to reply
1 reply to this topic

#1 OriginalSunny

OriginalSunny
  • Members
  • PipPipPip
  • Advanced Member
  • 95 posts

Posted 07 March 2006 - 01:26 PM

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.

sql_item: Duplicate entry '0' for key 1

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.


#2 Toni_montana

Toni_montana
  • Members
  • Pip
  • Newbie
  • 8 posts

Posted 07 March 2006 - 01:31 PM

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




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users