MySQL On Duplicate Key Update
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')
开发者_如何学C 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'
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.
精彩评论