-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaddCart.php
94 lines (42 loc) · 1.63 KB
/
addCart.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
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<?php
session_start();
require 'config.php';
if(isset($_SESSION['log_user'])){// check if the user is logged in
$cus_id=$_SESSION['log_user'];
if(isset($_POST['addCart'])){// check if the button
$sql="SELECT * FROM cart WHERE cus_id=$cus_id";//query cart id form cart table
$result=$conn->query($sql);
if($result->num_rows>0){
while($row=$result->fetch_assoc()){
$cart_id=$row['cart_id'];
}
$item_name=$_GET['item_name'];//get the item name form temDetails page using get Method
$quantity=$_POST['qty'];//get size and quantity from item details page
$size=$_POST['size'];
$sqli="SELECT * FROM item WHERE name='$item_name' AND size='$size'";//query the item from cart table
$item_result=$conn->query($sqli);
if($item_result->num_rows>0){
while($item_rows=$item_result->fetch_assoc()){
$itm_id=$item_rows['item_code'];
$price=$item_rows['unit_price'];
$item_qty=$item_rows['stock'];
//not enough stocks
}
if($quantity<=$item_qty){
$sql_add="INSERT INTO cart_details VALUES('$itm_id','$cart_id','$price','$size','$quantity')";//add the new cart items to cart
if($conn->query($sql_add)){
header("Location:cart.php");//Re direcrt to cart
}else{
echo "<script>alert('Item already in Cart')</script>";
}
}else{
echo "<script>alert('Not enough stock')</script>";//if quantity>stocks
}
}else{
echo "<script>alert('Not enough stock')</script>";//if item not available
}//no items
}
}//button not pressed
}else{
header("Location:login.php");
}//Direct to log in page