Files
CMS-Noam/admin/modif.php
2025-11-02 14:51:56 +01:00

58 lines
1.7 KiB
PHP

<?php
global $pdo;
require '../include/db.php';
require '../include/auth.php';
requireLogin();
$id = isset($_GET['id']) ? (int)$_GET['id'] : 0;
if ($id <= 0) {
header('Location: board.php');
exit;
}
$stmt = $pdo->prepare('SELECT * FROM articles WHERE id = :id');
$stmt->execute([':id' => $id]);
$article = $stmt->fetch();
if (!$article) {
header('Location: board.php');
exit;
}
$errors = [];
$titre = $article['titre'];
$contenu = $article['contenu'];
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$titre = trim($_POST['titre'] ?? '');
$contenu = trim($_POST['contenu'] ?? '');
if ($titre === '' || $contenu === '') {
$errors[] = 'Tous les champs sont obligatoires.';
} else {
$u = $pdo->prepare('UPDATE articles SET titre = :titre, contenu = :contenu WHERE id = :id');
$u->execute([':titre' => $titre, ':contenu' => $contenu, ':id' => $id]);
header('Location: board.php');
exit;
}
}
?>
<!doctype html>
<html lang="fr">
<head>
<meta charset="utf-8">
<title>Modifier l'article</title>
<link rel="stylesheet" href="/assets/style.css">
</head>
<body>
<h1>Modifier l'article</h1>
<?php foreach ($errors as $e): ?>
<p class="error"><?= htmlspecialchars($e) ?></p>
<?php endforeach; ?>
<form method="post">
<label>Titre<br><input type="text" name="titre" value="<?= htmlspecialchars($titre) ?>" required></label><br>
<label>Contenu<br><textarea name="contenu" rows="10" required><?= htmlspecialchars($contenu) ?></textarea></label><br>
<button type="submit">Enregistrer</button>
<a href="board.php">Annuler</a>
</form>
</body>
</html>