-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconection.php
More file actions
127 lines (96 loc) · 3.86 KB
/
conection.php
File metadata and controls
127 lines (96 loc) · 3.86 KB
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
<?php ob_start(); ?>
<script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<link rel="stylesheet" href="assets/bootstrap/css/bootstrap.min.css?h=f1d2fef1b7b619906014980fa4fbbe13">
<?php include ('dbconnect.php') ?>
<?php
if($_SERVER['REQUEST_METHOD'] == 'POST'){ //has the user submitted the form
if(empty($_POST["order_name"]) && strlen($_POST["order_name"]) == 0 || empty($_POST["Sprinkles"]) && strlen($_POST["Sprinkles"]) == 0 || empty($_POST["Chocolate"]) && strlen($_POST["Chocolate"]) == 0 || empty($_POST["Caramel"]) && strlen($_POST["Caramel"]) == 0 || empty($_POST["Raspberry"]) && strlen($_POST["Raspberry"]) == 0 || empty($_POST["Strawberry"]) && strlen($_POST["Strawberry"]) == 0 || empty($_POST["Blueberry"]) && strlen($_POST["Blueberry"]) == 0)
{
echo '
<script type="text/javascript">
$(document).ready(function(){
swal({
icon: "error",
title: "Oups, something is missing!",
text: "Check again for missing input"
})
});
</script>
';
}
else{
//dummy form data to insert into the database - imagine this was sent from a HTML form submission using POST method
$order_name = $_POST['order_name'];
$sprinkles = $_POST['Sprinkles'];
$chocolate = $_POST['Chocolate'];
$caramel = $_POST['Caramel'];
$raspberry = $_POST['Raspberry'];
$strawberry = $_POST['Strawberry'];
$blueberry = $_POST['Blueberry'];
// get user id
$user_id = $_SESSION["user_mail"];
// Declare Variables
$sprinkles_price = 1;
$chocolate_price = 1.20;
$caramel_price = 1;
$raspberry_price = 0.80;
$strawberry_price = 0.80;
$blueberry_price = 0.80;
// Calculate total items
$total = $sprinkles + $chocolate + $caramel + $raspberry + $strawberry + $blueberry;
// Get Current date function
$Date = Date("Y/m/d");
$sprinkles_total = ($sprinkles * $sprinkles_price);
$chocolate_total = ($chocolate * $chocolate_price);
$caramel_total = ($caramel * $caramel_price);
$raspberry_total = ($raspberry * $raspberry_price);
$strawberry_total = ($strawberry * $strawberry_price);
$blueberry_total = ($blueberry * $blueberry_price);
// Calculate total price
$total_price = $sprinkles_total + $chocolate_total + $caramel_total + $raspberry_total + $strawberry_total + $blueberry_total;
$total_price = number_format($total_price, 2);
if($total == 12){
$sql = "INSERT INTO donut (Order_Name, sprinkles, chocolate, caramel, raspberry, strawberry, blueberry, Total_Price, Order_Date, User_ID) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
$sth = $conn->prepare($sql);
$sth->execute([$order_name ,$sprinkles ,$chocolate ,$caramel ,$raspberry ,$strawberry ,$blueberry ,$total_price ,$Date ,$user_id]);
$last_id = $conn->lastInsertId();
echo '
<script type="text/javascript">
$(document).ready(function(){
swal({
icon: "success",
title: "Order was placed successfully",
text: "Total Donuts: 12",
text: "Total Price: '.$total_price.' " ,
})
});
</script>
';
//echo "Ordered donuts successfully, Last ID inserted: ID ". $last_id . '<br>'; // If successful we will see this
//echo "Total Items: " . $total . '<br>';
//echo "Total Price: " . $total_price;
}
else{
echo '
<script type="text/javascript">
$(document).ready(function(){
swal({
icon: "error",
title: "Oups, you reached the limit of 12 donuts",
text: "You are allowed to place an order only for 12 donuts!"
})
});
</script>
';
}
}
}
else{
echo '
<script type="text/javascript">
$(document).ready(function(){
</script>
';
}
?>