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

78 lines
3.1 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 = '';
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 {
$pdo = db();
$stmt = $pdo->prepare(
"INSERT INTO videos (user_id, title, description, video_url, creator, upload_date) VALUES (?, ?, ?, ?, ?, ?)"
);
// Using session user_id and current date
$stmt->execute([$_SESSION['user_id'], $title, $description, $video_url, $creator, date('Y-m-d')]);
header("Location: admin.php");
exit;
} catch (PDOException $e) {
$error_message = 'Database error: ' . $e->getMessage();
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Add 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>Add New Video</h2>
<?php if ($error_message): ?>
<div class="alert alert-danger"><?php echo $error_message; ?></div>
<?php endif; ?>
<form method="POST" action="video_add.php">
<div class="mb-3">
<label for="title" class="form-label">Title</label>
<input type="text" class="form-control" id="title" name="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></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" 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" required>
</div>
<button type="submit" class="btn btn-success">Add Video</button>
<a href="admin.php" class="btn btn-secondary">Cancel</a>
</form>
</div>
</body>
</html>