-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadmin.php
More file actions
212 lines (192 loc) · 8.27 KB
/
Copy pathadmin.php
File metadata and controls
212 lines (192 loc) · 8.27 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
<?php
// admin_panel.php
session_start();
require 'db_connect.php';
// Vérifier si l'utilisateur est admin
if (!isset($_SESSION['role']) || $_SESSION['role'] !== 'admin') {
header('Location: index.php');
exit();
}
// Gestion de l'ajout d'article
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['ajouter_article'])) {
$nom = $_POST['nom'];
$description = $_POST['description'];
$prix = $_POST['prix'];
$stock = $_POST['stock'];
$image = $_POST['image'];
try {
$stmt = $pdo->prepare("INSERT INTO produits (nom, description, prix, stock, image) VALUES (:nom, :description, :prix, :stock, :image)");
$stmt->execute([
':nom' => $nom,
':description' => $description,
':prix' => $prix,
':stock' => $stock,
':image' => $image
]);
$message = "Article ajouté avec succès.";
} catch (PDOException $e) {
$message = "Erreur : " . $e->getMessage();
}
}
// Gestion de la suppression d'article
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['supprimer_article'])) {
$id = $_POST['article_id'];
try {
$stmt = $pdo->prepare("DELETE FROM produits WHERE id = :id");
$stmt->execute([':id' => $id]);
$message = "Article supprimé avec succès.";
} catch (PDOException $e) {
$message = "Erreur : " . $e->getMessage();
}
}
// Gestion des annonces
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['ajouter_annonce'])) {
$titre = $_POST['titre'];
$contenu = $_POST['contenu'];
try {
$stmt = $pdo->prepare("INSERT INTO annonces (titre, contenu) VALUES (:titre, :contenu)");
$stmt->execute([':titre' => $titre, ':contenu' => $contenu]);
$message = "Annonce ajoutée avec succès.";
} catch (PDOException $e) {
$message = "Erreur : " . $e->getMessage();
}
}
// Suppression des annonces
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['supprimer_annonce'])) {
$id = $_POST['annonce_id'];
try {
$stmt = $pdo->prepare("DELETE FROM annonces WHERE id = :id");
$stmt->execute([':id' => $id]);
$message = "Annonce supprimée avec succès.";
} catch (PDOException $e) {
$message = "Erreur : " . $e->getMessage();
}
}
// Récupérer les articles et annonces
$articles = $pdo->query("SELECT * FROM produits ORDER BY id DESC")->fetchAll(PDO::FETCH_ASSOC);
$annonces = $pdo->query("SELECT * FROM annonces ORDER BY id DESC")->fetchAll(PDO::FETCH_ASSOC);
// Récupérer les logs
try {
$logs = $pdo->query("SELECT * FROM commandes ORDER BY date_creation DESC LIMIT 10")->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
$logs = [];
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Admin Panel</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<header class="bg-primary text-white p-3">
<h1>Admin Panel</h1>
</header>
<main class="container mt-5">
<!-- Gestion des articles -->
<section>
<h2>Gestion des Articles</h2>
<form method="POST" class="mb-5">
<input type="hidden" name="ajouter_article" value="1">
<div class="mb-3">
<label for="nom" class="form-label">Nom</label>
<input type="text" name="nom" id="nom" class="form-control" required>
</div>
<div class="mb-3">
<label for="description" class="form-label">Description</label>
<textarea name="description" id="description" class="form-control" required></textarea>
</div>
<div class="mb-3">
<label for="prix" class="form-label">Prix</label>
<input type="number" name="prix" id="prix" class="form-control" required>
</div>
<div class="mb-3">
<label for="stock" class="form-label">Stock</label>
<input type="number" name="stock" id="stock" class="form-control" required>
</div>
<div class="mb-3">
<label for="image" class="form-label">Image (URL)</label>
<input type="text" name="image" id="image" class="form-control">
</div>
<button type="submit" class="btn btn-primary">Ajouter</button>
</form>
<h3>Liste des Articles</h3>
<table class="table table-striped">
<thead>
<tr>
<th>#</th>
<th>Nom</th>
<th>Description</th>
<th>Prix</th>
<th>Stock</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php foreach ($articles as $article): ?>
<tr>
<td><?= $article['id'] ?></td>
<td><?= htmlspecialchars($article['nom']) ?></td>
<td><?= htmlspecialchars($article['description']) ?></td>
<td><?= $article['prix'] ?> €</td>
<td><?= $article['stock'] ?></td>
<td>
<form method="POST" style="display:inline;">
<input type="hidden" name="supprimer_article" value="1">
<input type="hidden" name="article_id" value="<?= $article['id'] ?>">
<button type="submit" class="btn btn-danger btn-sm">Supprimer</button>
</form>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</section>
<!-- Gestion des annonces -->
<section>
<h2>Gestion des Annonces</h2>
<form method="POST" class="mb-5">
<input type="hidden" name="ajouter_annonce" value="1">
<div class="mb-3">
<label for="titre" class="form-label">Titre</label>
<input type="text" name="titre" id="titre" class="form-control" required>
</div>
<div class="mb-3">
<label for="contenu" class="form-label">Contenu</label>
<textarea name="contenu" id="contenu" class="form-control" required></textarea>
</div>
<button type="submit" class="btn btn-primary">Ajouter Annonce</button>
</form>
<h3>Liste des Annonces</h3>
<table class="table table-striped">
<thead>
<tr>
<th>#</th>
<th>Titre</th>
<th>Contenu</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php foreach ($annonces as $annonce): ?>
<tr>
<td><?= $annonce['id'] ?></td>
<td><?= htmlspecialchars($annonce['titre']) ?></td>
<td><?= htmlspecialchars($annonce['contenu']) ?></td>
<td>
<form method="POST" style="display:inline;">
<input type="hidden" name="supprimer_annonce" value="1">
<input type="hidden" name="annonce_id" value="<?= $annonce['id'] ?>">
<button type="submit" class="btn btn-danger btn-sm">Supprimer</button>
</form>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</section>
</main>
</body>
</html>