-
Notifications
You must be signed in to change notification settings - Fork 0
/
addToCart.php
45 lines (39 loc) · 1.41 KB
/
addToCart.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<?php
require('./database.php');
session_start();
?>
<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
<?php
if (isset($_POST['add'])) {
$itemIdRaw = filter_input(INPUT_POST, 'itemId');
$itemId = $itemIdRaw[0];
$email = $_SESSION['email'];
$userQuery = "SELECT * FROM user WHERE email = :email";
$userPrep = $db->prepare($userQuery);
$userPrep->bindParam(':email', $email, PDO::PARAM_STR);
$userId = $userPrep->execute();
$numAlreadyInCartQuery = "SELECT * FROM cart WHERE cart.id = $userId AND cart.itemId = $itemId";
$temp =$db->query($numAlreadyInCartQuery);
$count = $temp->rowCount();
if ($count == 0){
$insertQuery = "INSERT INTO cart (id, itemId, quantity) VALUES (:userId, :itemId, 1)";
$insert = $db->prepare($insertQuery);
$insert->bindParam(':userId', $userId, PDO::PARAM_INT);
$insert->bindParam(':itemId', $itemId, PDO::PARAM_INT);
$result = $insert->execute();
} else {
$increaseQuantityQuery = "UPDATE cart SET quantity = quantity + 1 WHERE cart.id = :userId AND cart.itemId = :itemId";
$increaseQuantity = $db->prepare($increaseQuantityQuery);
$increaseQuantity->bindParam(':userId', $userId, PDO::PARAM_INT);
$increaseQuantity->bindParam(':itemId', $itemId, PDO::PARAM_INT);
$result = $increaseQuantity->execute();
}
header('location: cart.php');
}
?>
</body>
</html>