-
Notifications
You must be signed in to change notification settings - Fork 0
/
addtocart.php
71 lines (64 loc) · 2.29 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
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
<!DOCTYPE>
<html>
<script type="text/javascript">
function goback()
{
window.history.back();
}
</script>
<?php
ini_set('error_reporting', 'E_COMPILE_ERROR|E_RECOVERABLE_ERROR|E_ERROR|E_CORE_ERROR');
include 'connection.php';
$conn = OpenCon();
session_start();
if($_SESSION['logged_in'] == false)
{
echo '<sctipt>alert("You are not logged in. You must login to continue. ")</script>.';
echo '<meta http-equiv="refresh" content="0; URL=\'login.php\'" />';
exit;
}
$user_name=$_SESSION['username'];
$query='SELECT wallet from userss where user_id='.$user_name;
$result=mysqli_query($conn,$query);
$row=$result->fetch_assoc();
$wallet=$row['wallet'];
$_SESSION['wallet']=$wallet;
$product_id = $_GET['product_id'];
$quantity = $_POST['quantity'];
$print_pro = 'Product ID = '.$product_id.' \n Quantity = '.$quantity.' \n ';
$query = 'SELECT price from products where product_id='.$product_id;
$result=mysqli_query($conn,$query);
$row=$result->fetch_assoc();
$price=$row['price'];
$print_pro = $print_pro.'Price = '.$price.' \n ';
$total = $price*$quantity;
$ttotal=$total;
$print_pro = $print_pro.'Total Price = '.$total.' \n ';
if($ttotal > $wallet)
{
echo '<script> alert("You don\'t have enough money. Please recharge your account to buy products.");</script>';
echo '<script>window.history.back();</script>';
exit;
}
$query='SELECT quantity,total from cart WHERE user_id='.$user_name.' AND product_id='.$product_id;
$result=mysqli_query($conn,$query);
if($result->num_rows == 1)
{
$row=$result->fetch_assoc();
$q=$row['quantity'];
$t=$row['total'];
$quantity=$quantity+$q;
$total=$total+$t;
$query='DELETE from cart WHERE user_id='.$user_name.' AND product_id='.product_id;
$deleteresult=mysqli_query($conn,$query);
}
$query='INSERT INTO cart (user_id,product_id,quantity,total) VALUES('.$user_name.','.$product_id.','.$quantity.','.$total.')';
$result=mysqli_query($conn,$query);
$wallet=$wallet-$ttotal;
$_SESSION['wallet']=$wallet;
$query='UPDATE userss SET wallet='.$wallet.' WHERE user_id = '.$user_name;
$result=mysqli_query($conn,$query);
echo '<script>alert("'.$print_pro.' \n Product Added");</script>';
echo '<meta http-equiv="refresh" content="0; URL=\'ehome.php\'" /> ';
?>
</html>