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()]); } }