36945-vm/admin/edit.php
2025-12-14 19:49:54 +00:00

86 lines
2.5 KiB
PHP

<?php
session_start();
if (!isset($_SESSION['loggedin']) || $_SESSION['loggedin'] !== true) {
header('Location: index.php');
exit;
}
require_once __DIR__ . '/../db/config.php';
if (!isset($_GET['id'])) {
header('Location: dashboard.php');
exit;
}
$pdo = db();
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$title = $_POST['title'] ?? '';
$content = $_POST['content'] ?? '';
$id = $_GET['id'];
if (empty($title) || empty($content)) {
$error = 'Title and content are required';
} else {
$stmt = $pdo->prepare('UPDATE posts SET title = ?, content = ? WHERE id = ?');
$stmt->execute([$title, $content, $id]);
header('Location: dashboard.php');
exit;
}
} else {
$stmt = $pdo->prepare('SELECT * FROM posts WHERE id = ?');
$stmt->execute([$_GET['id']]);
$post = $stmt->fetch();
if (!$post) {
header('Location: dashboard.php');
exit;
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Edit Post</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<div class="container">
<a class="navbar-brand" href="dashboard.php">Admin</a>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav ms-auto">
<li class="nav-item">
<a class="nav-link" href="logout.php">Logout</a>
</li>
</ul>
</div>
</div>
</nav>
<div class="container py-5">
<h1>Edit Post</h1>
<?php if (isset($error)): ?>
<div class="alert alert-danger"><?php echo $error; ?></div>
<?php endif; ?>
<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="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">Save Changes</button>
<a href="dashboard.php" class="btn btn-secondary">Cancel</a>
</form>
</div>
</body>
</html>