jeffcodedev123
-
Posts
1 -
Joined
-
Last visited
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.
Check if record exists, update quantity if it does or insert new record otherwise
in MySQL Help
Posted · Edited by jeffcodedev123
error
Hello I need some help please!
I have a web page that currently inserts a record containing customerID, productID and quantity into the cart table if a user adds a product to their cart. However, if the user goes back to the shop and re-adds that same product to their cart. The php/mysql creates a new record regardless of what they have in their cart.
Current output in table:
customerID productID quantity
1 ABC12 3
1 ABC12 1
Expected output:
customerID productID quantity
1 ABC12 4
All help is greatly appreciated.
<!doctype html>
<html>
<head>
<title>Add Item to Cart</title>
<?php
session_start();
echo $_SESSION["user_id"];
$user_id = $_SESSION["user_id"];
$stockID = $_POST['stockNo'];
$quantity = $_POST['quantity'];
//connect to database.
include 'dbConnect.php';
//create MySQL insert command
$query1 = "insert into cart (customerID,stockID,quantity) values('$user_id','$stockID','$quantity');
//execute the MySQL statement
$result1 = mysqli_query($db,$query1);
?>
</head>
<body>
<?php include 'navBar.php'; ?>
<h1>Adding to cart....</h1>
<br>
<?php
if ($result1){
echo "Item has been added to your cart.";
}
else {
echo "Theres been a problem. Item was not added to your cart.";
}
?>
</body>
<?php //close connection to the database
mysqli_close();
?>
</html>