-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOrder.php
More file actions
37 lines (28 loc) · 1.12 KB
/
Order.php
File metadata and controls
37 lines (28 loc) · 1.12 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
<?php
require_once 'connect/connection.php';
class Order {
private $conn;
public function __construct($connection) {
$this->conn = $connection;
}
public function getInventory() {
$stmt = $this->conn->prepare("SELECT * FROM inventory");
$stmt->execute();
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
public function placeOrder($userId, $itemId, $quantity) {
$stmt = $this->conn->prepare("SELECT * FROM inventory WHERE id = ?");
$stmt->execute([$itemId]);
$item = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$item || $item['stock'] < $quantity) {
return "Insufficient stock!";
}
$totalPrice = $item['price'] * $quantity;
$insertOrder = $this->conn->prepare("INSERT INTO orders (user_id, item_id, quantity, total_price) VALUES (?, ?, ?, ?)");
$insertOrder->execute([$userId, $itemId, $quantity, $totalPrice]);
$updateStock = $this->conn->prepare("UPDATE inventory SET stock = stock - ? WHERE id = ?");
$updateStock->execute([$quantity, $itemId]);
return "Order placed successfully!";
}
}
?>