35682-vm/api/favorites.php
2025-11-13 12:17:45 +00:00

35 lines
1.1 KiB
PHP

<?php
session_start();
require_once __DIR__ . '/../db/config.php';
header('Content-Type: application/json');
if (!isset($_SESSION['user_id'])) {
echo json_encode(['success' => false, 'error' => 'User not logged in.']);
exit;
}
$data = json_decode(file_get_contents('php://input'), true);
$video_id = $data['video_id'] ?? null;
$title = $data['title'] ?? null;
$thumbnail = $data['thumbnail'] ?? null;
if (!$video_id || !$title || !$thumbnail) {
echo json_encode(['success' => false, 'error' => 'Invalid data.']);
exit;
}
try {
$pdo = db();
$stmt = $pdo->prepare("INSERT INTO favorites (user_id, video_id, title, thumbnail) VALUES (:user_id, :video_id, :title, :thumbnail)");
$stmt->execute(['user_id' => $_SESSION['user_id'], 'video_id' => $video_id, 'title' => $title, 'thumbnail' => $thumbnail]);
echo json_encode(['success' => true]);
} catch (PDOException $e) {
if ($e->errorInfo[1] == 1062) { // Duplicate entry
echo json_encode(['success' => false, 'error' => 'Already in favorites.']);
} else {
echo json_encode(['success' => false, 'error' => 'Database error: ' . $e->getMessage()]);
}
}