-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.php
More file actions
115 lines (99 loc) · 3.73 KB
/
db.php
File metadata and controls
115 lines (99 loc) · 3.73 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
<?php
require_once 'config.php';
class Database {
private $conn;
public function __construct() {
try {
$this->conn = new PDO(
"mysql:host=" . DB_HOST . ";dbname=" . DB_NAME,
DB_USER,
DB_PASS,
array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8")
);
$this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
die("Erreur de connexion : " . $e->getMessage());
}
}
public function addProduct($nom, $description, $prix, $stock) {
try {
$stmt = $this->conn->prepare(
"INSERT INTO products (nom, description, prix, stock)
VALUES (:nom, :description, :prix, :stock)"
);
$stmt->bindParam(':nom', $nom, PDO::PARAM_STR);
$stmt->bindParam(':description', $description, PDO::PARAM_STR);
$stmt->bindParam(':prix', $prix, PDO::PARAM_STR);
$stmt->bindParam(':stock', $stock, PDO::PARAM_INT);
$stmt->execute();
return $this->conn->lastInsertId();
} catch(PDOException $e) {
return ['error' => $e->getMessage()];
}
}
public function updateProduct($id, $nom, $description, $prix, $stock) {
try {
$stmt = $this->conn->prepare(
"UPDATE products
SET nom = :nom,
description = :description,
prix = :prix,
stock = :stock
WHERE id = :id"
);
$stmt->bindParam(':id', $id, PDO::PARAM_INT);
$stmt->bindParam(':nom', $nom, PDO::PARAM_STR);
$stmt->bindParam(':description', $description, PDO::PARAM_STR);
$stmt->bindParam(':prix', $prix, PDO::PARAM_STR);
$stmt->bindParam(':stock', $stock, PDO::PARAM_INT);
return $stmt->execute();
} catch(PDOException $e) {
return ['error' => $e->getMessage()];
}
}
public function deleteProduct($id) {
try {
$stmt = $this->conn->prepare("DELETE FROM products WHERE id = :id");
$stmt->bindParam(':id', $id, PDO::PARAM_INT);
return $stmt->execute();
} catch(PDOException $e) {
return ['error' => $e->getMessage()];
}
}
public function getAllProducts() {
try {
$stmt = $this->conn->query("SELECT * FROM products ORDER BY created_at DESC");
return $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch(PDOException $e) {
return ['error' => $e->getMessage()];
}
}
public function searchProducts($term) {
try {
$stmt = $this->conn->prepare(
"SELECT * FROM products
WHERE nom LIKE :term
OR description LIKE :term"
);
$searchTerm = "%{$term}%";
$stmt->bindParam(':term', $searchTerm, PDO::PARAM_STR);
$stmt->execute();
return $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch(PDOException $e) {
return ['error' => $e->getMessage()];
}
}
public function filterByStock($minStock) {
try {
$stmt = $this->conn->prepare(
"SELECT * FROM products
WHERE stock >= :minStock"
);
$stmt->bindParam(':minStock', $minStock, PDO::PARAM_INT);
$stmt->execute();
return $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch(PDOException $e) {
return ['error' => $e->getMessage()];
}
}
}