-
Notifications
You must be signed in to change notification settings - Fork 16
/
checkout.php
66 lines (57 loc) · 2.13 KB
/
checkout.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
<?php
session_start();
require_once "includes/connection.php";
if(mysqli_connect_error()) {
echo "<script>
alert('UNKNOWN ISSUE: cannot process your request.');
window.location.href='menu.php';
</script>";
exit();
}
if($_SERVER['REQUEST_METHOD']=='POST') {
if(isset($_POST['checkout'])) {
// Check if user is logged in
if(isset($_SESSION['logged_in']) && $_SESSION['logged_in'] == true) {
// Retrieve user information from registered_users table
$query = "SELECT * FROM registered_users WHERE email='{$_SESSION['username']}'";
$result = mysqli_query($conn, $query);
if($result && mysqli_num_rows($result) > 0) {
$user_data = mysqli_fetch_assoc($result);
// Use user data in the INSERT query for orders table
$name = $user_data['name'];
$email = $user_data['email'];
$state = $user_data['state'];
$district = $user_data['district'];
$address = $state . ' ' . $district;
// Loop through the arrays of items, prices, and quantities
$items = $_POST['Item_name'];
$prices = $_POST['price'];
$quantities = $_POST['Quantity'];
for($i = 0; $i < count($items); $i++) {
$item = $items[$i];
$price = $prices[$i];
$quantity = $quantities[$i];
$total_price = $price * $quantity; // Calculate total price
$query1 = "INSERT INTO `orders`(`name`, `email`, `address`,`item`, `quantity`, `total_price`)
VALUES ('$name','$email','$address','$item','$quantity','$total_price')";
if(mysqli_query($conn, $query1)) {
// Order placed successfully
} else {
// Error occurred while placing the order
echo "Error: " . mysqli_error($conn);
}
}
echo "<script>
alert('Orders placed successfully');
window.location.href='menu.php';
</script>";
}
} else {
echo "<script>
alert('Please login to proceed with checkout.');
window.location.href='login.php';
</script>";
}
}
}
?>