34786-vm/eliminar_info_producto.php
2025-12-12 16:33:10 +00:00

34 lines
1.1 KiB
PHP

<?php
require_once 'db/config.php';
if (!isset($_GET['id']) || empty($_GET['id'])) {
header('Location: info_producto.php');
exit;
}
$info_id = $_GET['id'];
$pdo = db();
// First, get the image URL to delete the file
$stmt_select = $pdo->prepare('SELECT imagen_url FROM info_productos WHERE id = :id');
$stmt_select->execute([':id' => $info_id]);
$info = $stmt_select->fetch(PDO::FETCH_ASSOC);
if ($info) {
// Delete the database record
$stmt_delete = $pdo->prepare('DELETE FROM info_productos WHERE id = :id');
if ($stmt_delete->execute([':id' => $info_id])) {
// If DB deletion is successful, delete the image file
if (file_exists($info['imagen_url'])) {
unlink($info['imagen_url']);
}
$_SESSION['success_message'] = 'La tarjeta de información ha sido eliminada.';
} else {
$_SESSION['error_message'] = 'No se pudo eliminar la tarjeta de la base de datos.';
}
} else {
$_SESSION['error_message'] = 'No se encontró la tarjeta especificada.';
}
header('Location: info_producto.php');
exit;