0

It is only allowing me to update resulting in a total of 2

Heres my Table

CREATE TABLE `cart` (
  `id` int(7) NOT NULL AUTO_INCREMENT,
  `User` int(7) DEFAULT NULL,
  `Product` varchar(100) DEFAULT NULL,
  `Quantity` int(7) DEFAULT NULL,
  UNIQUE KEY `id` (`id`),
  UNIQUE KEY `Quantity` (`Quantity`)
 )

Then, my code to insert the data is:

$a = '1'

query2 = "      INSERT INTO CART(User, Product,Quantity)
                VALUES
                ('$id','$model_number','$a')
                 ON DUPLICATE KEY UPDATE Quantity=Quantity+1";

It will work when I Add the data to the database but, upon my second attemp I get this error:

Duplicate entry '2' for key 'Quantity'

1 Answer 1

2

You probably don't want this line:

  UNIQUE KEY `Quantity` (`Quantity`)

That creates a unique constraint on the quantity field, which is why your second insert is failing. I can't think of any reason why you would want that.

P.S. If you remove that line, make sure to remove the comma (,) from the previous line.

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

3 Comments

Was the last statement a joke? That is extremely obvious
@RPM You would be surprised what's not obvious to many people.
Or use remove the UNIQUE part if you still want to do fast lookups by Quantity.

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.