-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInventoryController.php
More file actions
64 lines (58 loc) · 2.08 KB
/
InventoryController.php
File metadata and controls
64 lines (58 loc) · 2.08 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
<?php
class InventoryController {
private $pdo;
// Constructor to initialize PDO
public function __construct($pdo) {
if (!$pdo) {
die("Error: Database connection not established.");
}
$this->pdo = $pdo;
}
// Fetch all products from inventory
public function getAllProducts() {
try {
$stmt = $this->pdo->query("SELECT * FROM inventory");
return $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
die("Error fetching products: " . $e->getMessage());
}
}
// Add a new product to inventory
public function addProduct($item_name, $stock, $price) {
try {
$stmt = $this->pdo->prepare("INSERT INTO inventory (item_name, stock, price) VALUES (?, ?, ?)");
return $stmt->execute([$item_name, $stock, $price]);
} catch (PDOException $e) {
die("Error adding product: " . $e->getMessage());
}
}
// Update an existing product in inventory
public function updateProduct($id, $item_name, $stock, $price) {
try {
$stmt = $this->pdo->prepare("UPDATE inventory SET item_name = ?, stock = ?, price = ? WHERE id = ?");
return $stmt->execute([$item_name, $stock, $price, $id]);
} catch (PDOException $e) {
die("Error updating product: " . $e->getMessage());
}
}
// Delete a product from inventory
public function deleteProduct($id) {
try {
$stmt = $this->pdo->prepare("DELETE FROM inventory WHERE id = ?");
return $stmt->execute([$id]);
} catch (PDOException $e) {
die("Error deleting product: " . $e->getMessage());
}
}
// Fetch a product by ID
public function getProductById($id) {
try {
$stmt = $this->pdo->prepare("SELECT * FROM inventory WHERE id = ?");
$stmt->execute([$id]);
return $stmt->fetch(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
die("Error fetching product: " . $e->getMessage());
}
}
}
?>