36439-vm/video_edit.php
2025-11-28 19:14:48 +00:00

95 lines
3.6 KiB
PHP

<?php
session_start();
require_once __DIR__ . '/db/config.php';
// If the user is not logged in or not an admin, redirect them.
if (!isset($_SESSION['user_id']) || !$_SESSION['is_admin']) {
header("Location: login.php");
exit;
}
$error_message = '';
$video_id = $_GET['id'] ?? null;
if (!$video_id) {
header("Location: admin.php");
exit;
}
$pdo = db();
// Handle form submission
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$title = $_POST['title'] ?? '';
$description = $_POST['description'] ?? '';
$video_url = $_POST['video_url'] ?? '';
$creator = $_POST['creator'] ?? '';
if (empty($title) || empty($description) || empty($video_url) || empty($creator)) {
$error_message = 'Please fill in all fields, including the Video URL.';
} else {
try {
$stmt = $pdo->prepare(
"UPDATE videos SET title = ?, description = ?, video_url = ?, creator = ? WHERE id = ?"
);
$stmt->execute([$title, $description, $video_url, $creator, $video_id]);
header("Location: admin.php");
exit;
} catch (PDOException $e) {
$error_message = 'Database error: ' . $e->getMessage();
}
}
}
// Fetch existing video data
$stmt = $pdo->prepare("SELECT * FROM videos WHERE id = ?");
$stmt->execute([$video_id]);
$video = $stmt->fetch();
if (!$video) {
header("Location: admin.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 Video | MyTube Admin</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="https://fonts.googleapis.com/css2?family=Roboto:wght@400;700&display=swap" rel="stylesheet">
<link rel="stylesheet" href="assets/css/custom.css?v=<?php echo time(); ?>">
</head>
<body>
<div class="container mt-5">
<h2>Edit Video</h2>
<?php if ($error_message): ?>
<div class="alert alert-danger"><?php echo $error_message; ?></div>
<?php endif; ?>
<form method="POST" action="video_edit.php?id=<?php echo $video['id']; ?>">
<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($video['title']); ?>" required>
</div>
<div class="mb-3">
<label for="description" class="form-label">Description</label>
<textarea class="form-control" id="description" name="description" rows="3" required><?php echo htmlspecialchars($video['description']); ?></textarea>
</div>
<div class="mb-3">
<label for="video_url" class="form-label">Video URL</label>
<input type="url" class="form-control" id="video_url" name="video_url" value="<?php echo htmlspecialchars($video['video_url']); ?>" required>
<small class="form-text text-muted">Please provide a direct link to the video (e.g., a YouTube embed link).</small>
</div>
<div class="mb-3">
<label for="creator" class="form-label">Creator</label>
<input type="text" class="form-control" id="creator" name="creator" value="<?php echo htmlspecialchars($video['creator']); ?>" required>
</div>
<button type="submit" class="btn btn-success">Save Changes</button>
<a href="admin.php" class="btn btn-secondary">Cancel</a>
</form>
</div>
</body>
</html>