-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathorder.php
More file actions
68 lines (66 loc) · 2 KB
/
Copy pathorder.php
File metadata and controls
68 lines (66 loc) · 2 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
<?php
// Returns NULL on order placed, else returns 1
function OrderPlaced(){
require 'sessionStart.php';
require 'db.php';
require 'user.php';
if(isUserBlacklisted() == true)
throw new Exception('User is blacklisted');
$sql = 'INSERT INTO orders (userID,workerID,shipped)VALUES (:userID,:workerID,:shipped)';
$sth = $db->prepare ($sql);
$sth->bindValue (':userID', $_SESSION['id']);
$sth->bindValue (':workerID',null);
$sth->bindValue (':shipped',0);
$sth->execute ();
// Get ID of last insert
$orderID = $db->lastInsertId();
$ordersplaced = 0;
$shouldHaveBeenDone = 0;
foreach ($_SESSION as $key => $quantity){
if((substr($key,0,1) != "p"))
continue;
$shouldHaveBeenDone++;
// Get price from DB
$sqltmp = 'Select * from products where id = :id';
$sthTmp = $db->prepare ($sqltmp);
$sthTmp->bindValue(':id',substr($key,1));
$sthTmp->execute ();
$sthTmp->setFetchMode(PDO::FETCH_ASSOC);
$row = $sthTmp->fetch();
if(intval($row['rabatt']) != 0)
$price = (intval($row['price'])*(1-(intval($row['rabatt'])/100)));
else
$price = $row['price'];
$sql = 'INSERT INTO orderdetail (orderID, productID, price, qty, sendt)VALUES (:orderID, :productID, :price, :qty, :sendt)';
$sth = $db->prepare ($sql);
$sth->bindValue (':orderID', $orderID);
$sth->bindValue (':productID', substr($key,1));
$sth->bindValue (':price', $price);
$sth->bindValue (':qty', $quantity);
$sth->bindValue (':sendt', "0");
$ordersplaced += $sth->execute ();
}
if($ordersplaced === $shouldHaveBeenDone && $shouldHaveBeenDone != 0){
emptyBasket();
}else{
throw new Exception(' Order not placed, something went wrong');
}
}
?>
<?php
include 'Geeksforsaletop.php';
?>
<div id="content">
<?php
if(isset($_POST['placed'])){
try {
OrderPlaced();
echo "<p> Thanks for your order";
} catch (Exception $e){
echo $e->getMessage();
}
}
?>
</div>
</BODY>
</HTML>