34849-vm/delete_info_producto.php
2026-02-03 01:43:03 +00:00

50 lines
1.3 KiB
PHP

<?php
session_start();
if (!isset($_SESSION['user_id'])) {
header('Location: login.php');
exit;
}
require_once 'db/config.php';
if (!isset($_GET['id']) || empty($_GET['id'])) {
$_SESSION['error_message'] = 'ID de tarjeta no válido.';
header('Location: info_producto.php');
exit;
}
$id = filter_input(INPUT_GET, 'id', FILTER_VALIDATE_INT);
if (!$id) {
$_SESSION['error_message'] = 'ID de tarjeta no válido.';
header('Location: info_producto.php');
exit;
}
$pdo = db();
try {
// First, get the image path to delete the file
$stmt = $pdo->prepare('SELECT imagen_url FROM info_productos WHERE id = ?');
$stmt->execute([$id]);
$imagen_url = $stmt->fetchColumn();
// Delete the record from the database
$stmt = $pdo->prepare('DELETE FROM info_productos WHERE id = ?');
$stmt->execute([$id]);
// If there was an image, delete the file
if ($imagen_url && file_exists($imagen_url)) {
unlink($imagen_url);
}
$_SESSION['success_message'] = 'La tarjeta de información ha sido eliminada con éxito.';
} catch (PDOException $e) {
error_log("Database error: " . $e->getMessage());
$_SESSION['error_message'] = 'Error al eliminar la tarjeta. Por favor, contacta a soporte.';
}
header('Location: info_producto.php');
exit;
?>