-
Notifications
You must be signed in to change notification settings - Fork 1
/
card.php
144 lines (108 loc) · 3.87 KB
/
card.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php
session_start();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<?php include 'plugins.php';?>
</head>
<body>
<div class="container">
<?php include 'header.php';?>
<div id="main">
<table class="table table-hover">
<thead>
<tr>
<th>Product</th>
<th>Quantity</th>
<th>Rate</th>
<th>Amount</th>
<th></th>
</tr>
</thead>
<tbody>
<?php
$servername="localhost";
$username="root";
$password="";
$dbname="shopping";
$conn=mysqli_connect($servername,$username,$password,$dbname);
if(!$conn){
die("connection failed:" . mysqli_connect_error());
}
$sql = "SELECT c.*, p.* FROM cart as c inner join products p on p.productID = c.productID WHERE c.sessionID=?";
$sessionID = session_id();
$stmt = $conn->stmt_init();
$stmt->prepare($sql);
$stmt->bind_param("s", $sessionID);
$stmt->execute();
$result = $stmt->get_result();
$totalamount = 0;
while($resultArray = $result->fetch_assoc())
{
echo '<tr>';
echo '<td>'.$resultArray['pname'].'</td>';
echo '<td>'.$resultArray['quantity'].'</td>';
echo '<td>₹'.$resultArray['rate'].'</td>';
echo '<td>₹'.$resultArray['amount'].'</td>';
echo '<td>';
echo '<a href="#"><i class="fa fa-trash fa-lg text-danger"></i>';
echo '</a>';
echo '</td>';
echo '</tr>';
$totalamount += $resultArray['amount'];
}
?>
</tbody>
</table>
<div id="checkout">
<p class="lead text-right">
Total Amount: <i class="fa fa-inr fa-lg"></i><?php echo $totalamount; ?>/-
</p>
<form class="float-right" action="<?php $_SERVER['PHP_SELF']; ?>" method="POST">
<input type="submit" class="btn btn-primary" value="Checkout">
</form>
<?php
if($_SERVER["REQUEST_METHOD"]=="POST")
{
$servername="localhost";
$username="root";
$password="";
$dbname="shopping";
$conn=mysqli_connect($servername,$username,$password,$dbname);
if(!$conn){
die("connection failed:" . mysqli_connect_error());
}
$userID = 1; //$_SESSION['userID'];
$orderDate = date("Y-m-d");
$sql="insert into orders (userID, TotalAmount, orderDate) values ('$userID', '$totalamount', '$orderDate')";
if(mysqli_query($conn,$sql))
{
$orderID = mysqli_insert_id($conn);
}
$sql = "INSERT INTO order_items (orderID, productID, quantity) SELECT '$orderID' as orderID, productID, quantity FROM cart WHERE sessionID = '$sessionID'";
mysqli_query($conn,$sql);
$sql = "delete from cart where sessionID = '$sessionID'";
if (mysqli_query($conn,$sql))
{
echo "<script>swal('Checkout','Your order has been confirmed.','info').then(() => { window.location.href='index.php'; });</script>";
}
else
{
echo"Error:" .mysqli_error($conn);
}
mysqli_close($conn);
}
?>
</div>
</div>
<br>
<br>
<?php include 'footer.php';?>
</div>
</body>
</html>