80 lines
2.4 KiB
PHP
80 lines
2.4 KiB
PHP
<?php
|
|
session_start();
|
|
|
|
if (!isset($_SESSION['user_id'])) {
|
|
header('Location: login.php');
|
|
exit;
|
|
}
|
|
|
|
$pageTitle = 'Edit Post';
|
|
require_once __DIR__ . '/includes/header.php';
|
|
require_once __DIR__ . '/db/config.php';
|
|
|
|
$post_id = $_GET['id'] ?? null;
|
|
$message = '';
|
|
$post = null;
|
|
|
|
if (!$post_id) {
|
|
header('Location: admin.php');
|
|
exit;
|
|
}
|
|
|
|
try {
|
|
$pdo = db();
|
|
$stmt = $pdo->prepare("SELECT * FROM posts WHERE id = ?");
|
|
$stmt->execute([$post_id]);
|
|
$post = $stmt->fetch();
|
|
} catch (PDOException $e) {
|
|
$message = '<div class="alert alert-danger">Error: ' . $e->getMessage() . '</div>';
|
|
}
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$title = $_POST['title'] ?? '';
|
|
$content = $_POST['content'] ?? '';
|
|
$author = $_POST['author'] ?? '';
|
|
|
|
if ($title && $content && $author) {
|
|
try {
|
|
$pdo = db();
|
|
$stmt = $pdo->prepare("UPDATE posts SET title = ?, content = ?, author = ? WHERE id = ?");
|
|
$stmt->execute([$title, $content, $author, $post_id]);
|
|
header('Location: admin.php');
|
|
exit;
|
|
} catch (PDOException $e) {
|
|
$message = '<div class="alert alert-danger">Error: ' . $e->getMessage() . '</div>';
|
|
}
|
|
} else {
|
|
$message = '<div class="alert alert-danger">Please fill in all fields.</div>';
|
|
}
|
|
}
|
|
|
|
if (!$post) {
|
|
echo "<div class='alert alert-danger'>Post not found.</div>";
|
|
require_once __DIR__ . '/includes/footer.php';
|
|
exit;
|
|
}
|
|
?>
|
|
|
|
<h1>Edit Post</h1>
|
|
|
|
<?php echo $message; ?>
|
|
|
|
<form method="POST">
|
|
<div class="mb-3">
|
|
<label for="title" class="form-label">Title</label>
|
|
<input type="text" class="form-control" id="title" name="title" value="<?php echo htmlspecialchars($post['title']); ?>" required>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="author" class="form-label">Author</label>
|
|
<input type="text" class="form-control" id="author" name="author" value="<?php echo htmlspecialchars($post['author']); ?>" required>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="content" class="form-label">Content</label>
|
|
<textarea class="form-control" id="content" name="content" rows="10" required><?php echo htmlspecialchars($post['content']); ?></textarea>
|
|
</div>
|
|
<button type="submit" class="btn btn-primary">Update Post</button>
|
|
<a href="admin.php" class="btn btn-secondary">Cancel</a>
|
|
</form>
|
|
|
|
<?php require_once __DIR__ . '/includes/footer.php';
|