-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadditem.php
More file actions
78 lines (65 loc) · 2.63 KB
/
additem.php
File metadata and controls
78 lines (65 loc) · 2.63 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<?php
session_start();
$car = $_POST['carid'];
$addqty = $_POST['qty'];
if (isset($_SESSION['memberid'])) {
$memberid = $_SESSION['memberid'];
}
additem();
function additem() {
global $memberid, $car, $addqty;
// Create database connection.
$config = parse_ini_file('../../private/db-config.ini');
$conn = new mysqli($config['servername'], $config['username'], $config['password'], 'project1004');
// Check connection
if ($conn->connect_error) {
$errorMsg = "Connection failed: " . $conn->connect_error;
$result = 0;
} else {
// check if cart have same car inside
$stmtcheckcar = $conn->prepare("SELECT *, cart.id as cartid from cart inner join car ON cart.car_id = car.id where member_id = ? and car_id= ?");
$stmtcheckcar->bind_param("ii", $memberid, $car);
$stmtcheckcar->execute();
$result = $stmtcheckcar->get_result();
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
$caridincart = $row['car_id'];
$caridinstore = $row['id'];
$cartid = $row['cartid'];
$qty = $row['qty'];
$stock = $row['stock'];
if ($caridincart == $caridinstore) {
$newqty = $qty + $addqty;
echo $newqty < $stock;
if ($newqty < $stock) {
// if same car is inside update qty
$stmtupdate = $conn->prepare("UPDATE cart SET qty = ? WHERE id= ? ");
$stmtupdate->bind_param("ii", $newqty, $cartid);
$stmtupdate->execute();
if ($stmtupdate >= 1) {
echo '<script>window.location.href = "cart.php";</script>';
} else {
echo "Cannot update qty";
}
} else {
// if qty is greater than stock
echo '<script>window.location.href = "cart.php";</script>';
}
}
}
} else {
$stmtinsert = $conn->prepare("INSERT INTO cart(member_id,car_id,qty) VALUES(?,?,?)");
$stmtinsert->bind_param("iii", $memberid, $car, $addqty);
$stmtinsert->execute();
if ($stmtinsert >= 1) {
echo '<script>window.location.href = "cart.php";</script>';
} else {
echo "Cannot insert";
}
$stmtinsert->close();
}
}
$conn->close();
return $result;
}
?>