38395-vm/toggle-favorite.php
Flatlogic Bot 4a91088287 sadiq
2026-02-13 15:07:39 +00:00

26 lines
693 B
PHP

<?php
require_once 'db/config.php';
session_start();
if (!isset($_SESSION['user_id'])) {
header('Location: ' . APP_URL . 'login.php');
exit;
}
$user_id = $_SESSION['user_id'];
$car_id = $_GET['id'] ?? 0;
$action = $_GET['action'] ?? 'add';
if ($car_id) {
$pdo = db();
if ($action === 'add') {
$stmt = $pdo->prepare("INSERT IGNORE INTO favorites (user_id, car_id) VALUES (?, ?)");
$stmt->execute([$user_id, $car_id]);
} else {
$stmt = $pdo->prepare("DELETE FROM favorites WHERE user_id = ? AND car_id = ?");
$stmt->execute([$user_id, $car_id]);
}
}
header('Location: ' . ($_SERVER['HTTP_REFERER'] ?? APP_URL . 'favorites.php'));
exit;