Define customer/product as UNIQUE...
CREATE TABLE `cart` (
`cart_id` int(11) NOT NULL AUTO_INCREMENT,
`customerID` int(11) DEFAULT NULL,
`productID` varchar(5) DEFAULT NULL,
`quantity` int(11) DEFAULT NULL,
PRIMARY KEY (`cart_id`),
UNIQUE KEY `UNQ_cart_product` (`customerID`,`productID`)
)
BEFORE
+---------+------------+-----------+----------+
| cart_id | customerID | productID | quantity |
+---------+------------+-----------+----------+
| 1 | 1 | ABC12 | 3 |
| 2 | 1 | DEF23 | 5 |
+---------+------------+-----------+----------+
UPDATE
INSERT INTO cart (customerID, productID, quantity)
VALUES
(1, 'ABC12', 2),
(1, 'DEF23', 5),
(1, 'GHI34', 10)
ON DUPLICATE KEY UPDATE
quantity = quantity + VALUES(quantity);
AFTER
+---------+------------+-----------+----------+
| cart_id | customerID | productID | quantity |
+---------+------------+-----------+----------+
| 1 | 1 | ABC12 | 5 |
| 2 | 1 | DEF23 | 10 |
| 3 | 1 | GHI34 | 10 |
+---------+------------+-----------+----------+