theloftstore/public/userarea/delete_template_xls.php
2025-02-26 17:29:27 +01:00

50 lines
1.3 KiB
PHP

<?php
// Abilita la gestione degli errori
ini_set('display_errors', 1);
error_reporting(E_ALL);
// Includi la connessione al database
require_once 'class/db-functions.php';
try {
// Controlla se è stato passato un ID valido
if (!isset($_GET['id']) || !is_numeric($_GET['id'])) {
throw new Exception('Invalid ID');
}
$id = intval($_GET['id']); // Sanifica l'ID
// Connessione al database
$db = DBHandlerSelect::getInstance();
$pdo = $db->getConnection();
if (!$pdo) {
throw new Exception('Database connection failed');
}
// Verifica se l'ID esiste
$stmtCheck = $pdo->prepare("SELECT id FROM excel_templates WHERE id = ?");
$stmtCheck->execute([$id]);
if ($stmtCheck->rowCount() === 0) {
throw new Exception('Template not found');
}
// Esegui l'eliminazione
$stmt = $pdo->prepare("DELETE FROM excel_templates WHERE id = ?");
$stmt->execute([$id]);
if ($stmt->rowCount() > 0) {
$message = "Template deleted successfully!";
$status = "success";
} else {
throw new Exception('Failed to delete template');
}
} catch (Exception $e) {
$message = $e->getMessage();
$status = "error";
}
// Reindirizza con un messaggio
header("Location: templates_dashboard.php?status=$status&message=" . urlencode($message));
exit;