-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcart.php
150 lines (126 loc) · 4.32 KB
/
cart.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
145
146
147
148
149
150
<!DOCTYPE html>
<html>
<?php
ob_start();
session_start();
$_SESSION['permission'] = '';
error_reporting(E_ERROR | E_PARSE);
$serverName = "DESKTOP-4OJ4H1R\SQLEXPRESS"; //serverName\instanceName
// Since UID and PWD are not specified in the $connectionInfo array,
// The connection will be attempted using Windows Authentication.
$connectionInfo = array( "Database"=>"TSQL");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
?>
<head>
<link rel = "stylesheet" href = "style.css">
</head>
<body>
<a href = "login.php" tite = "main page" style="text-decoration:none" ><h1>Herb shop</h1></a>
<form method="post" style="float:left">
Go to <a href = "welcome.php" tite = "Logout">main page</a>
</form>
<form method="post" style="float:right">
Click here to clean session - <a href = "logout.php" tite = "Logout">Logout</a>
</form><br><br>
<h3>Your cart list:</h3>
<?php
$sql = "SELECT product1, product2, product3 FROM Shop
WHERE login = '{$_SESSION["name"]}';";
$stmt = sqlsrv_query( $conn, $sql );
$actual_cart = sqlsrv_fetch_array($stmt);
foreach($actual_cart as $r) {}
sqlsrv_free_stmt($stmt);
$sql = "SELECT Name, Cost, Description FROM Products
Where ProductID = 1;";
$stmt = sqlsrv_query( $conn, $sql );
$item1 = sqlsrv_fetch_array($stmt);
foreach($item1 as $r) {}
sqlsrv_free_stmt($stmt);
$sql = "SELECT Name, Cost, Description FROM Products
Where ProductID = 2;";
$stmt = sqlsrv_query( $conn, $sql );
$item2 = sqlsrv_fetch_array($stmt);
foreach($item2 as $r) {}
sqlsrv_free_stmt($stmt);
$sql = "SELECT Name, Cost, Description FROM Products
Where ProductID = 3;";
$stmt = sqlsrv_query( $conn, $sql );
$item3 = sqlsrv_fetch_array($stmt);
foreach($item3 as $r) {}
sqlsrv_free_stmt($stmt);
echo "<h3><table border='1' style='background-color:white;'>
<tr>
<th>Product name</th>
<th>Cost</th>
<th>Amount</th>
<th>Sum</th>
</tr>";
{
if ($actual_cart[0]!=0){
echo "<tr>";
echo "<td>" . $item1[0] . "</td>";
echo "<td>$" . $item1[1] . "</td>";
echo "<td>" . $actual_cart[0] . "</td>";
echo "<td>$" . $item1[1]*$actual_cart[0] . "</td>";
echo "</tr>";
}
if ($actual_cart[1]!=0){
echo "<tr>";
echo "<td>" . $item2[0] . "</td>";
echo "<td>$" . $item2[1] . "</td>";
echo "<td>" . $actual_cart[1] . "</td>";
echo "<td>$" . $item2[1]*$actual_cart[1] . "</td>";
echo "</tr>";
}
if ($actual_cart[2]!=0){
echo "<tr>";
echo "<td>" . $item3[0] . "</td>";
echo "<td>$" . $item3[1] . "</td>";
echo "<td>" . $actual_cart[2] . "</td>";
echo "<td>$" . $item3[1]*$actual_cart[2] . "</td>";
echo "</tr>";
}
echo "<tr style='background-color:#90ff90;'>";
echo "<td>" . '' . "</td>";
echo "<td>" . '' . "</td>";
echo "<td>" . $actual_cart[0]+$actual_cart[1]+$actual_cart[2] . "</td>";
echo "<td>$" . $item1[1]*$actual_cart[0]+$item2[1]*$actual_cart[1]+$item3[1]*$actual_cart[2] . "</td>";
echo "</tr>";
}
echo "</table>";
?>
<form method="post">
<input type="submit" VALUE="Clear cart" name="clear_cart">
<input type="submit" VALUE="Order and pay" name="order_and_pay">
</form></h3>
<?php
if (isset($_POST['clear_cart'])){
$_SESSION['counter_product1'] = 0;
$_SESSION['counter_product2'] = 0;
$_SESSION['counter_product3'] = 0;
$sql = "UPDATE Shop
SET product1 = 0, product2 = 0, product3 = 0
WHERE login = '{$_SESSION["name"]}';";
$stmt = sqlsrv_query( $conn, $sql );
sqlsrv_free_stmt($stmt);
$_SESSION['empty_cart'] = 1;
header("Refresh:0");
}
if ($_SESSION['counter_product1'] == 0&&
$_SESSION['counter_product2'] == 0&&
$_SESSION['counter_product3'] == 0){
echo "<h3>Cart is empty!</h3>";}
if (isset($_POST['order_and_pay']) && ($_SESSION['counter_product1'] != 0 ||
$_SESSION['counter_product2'] != 0 ||
$_SESSION['counter_product3'] != 0)){
$ordered_cost = $item1[1]*$actual_cart[0]+$item2[1]*$actual_cart[1]+$item3[1]*$actual_cart[2];
$sql = "UPDATE Shop
SET ordered_cart_cost = '$ordered_cost', order_date = CAST(GETDATE() AS varchar(255)), PR1 = '{$_SESSION['counter_product1']}', PR2 = '{$_SESSION['counter_product2']}', PR3 = '{$_SESSION['counter_product3']}'
WHERE login = '{$_SESSION["name"]}';";
$stmt = sqlsrv_query( $conn, $sql );
sqlsrv_free_stmt($stmt);
echo "<h3>Your Order Was Successful! Thank you for your payment</h3>";
}
?>
</body>
</html>