36782-vm/admin/toggle_active.php
2025-12-12 14:13:03 +00:00

32 lines
696 B
PHP

<?php
require_once __DIR__ . '/../includes/auth.php';
require_role('admin');
require_once __DIR__ . '/../db/config.php';
if (!isset($_GET['id'])) {
header("Location: products.php");
exit;
}
$pdo = db();
$id = $_GET['id'];
// First, get the current status
$stmt = $pdo->prepare("SELECT is_active FROM products WHERE id = ?");
$stmt->execute([$id]);
$current_status = $stmt->fetchColumn();
if ($current_status === false) {
die("Produkt nie znaleziony.");
}
// Flip the status
$new_status = $current_status ? 0 : 1;
$update_stmt = $pdo->prepare("UPDATE products SET is_active = ? WHERE id = ?");
$update_stmt->execute([$new_status, $id]);
header("Location: products.php");
exit;