55ok
| Direktori : /home/u307599615/domains/costumeonrent.in/public_html/admin/ |
| Current File : /home/u307599615/domains/costumeonrent.in/public_html/admin/deleteslider.php |
<?php
include("connection.php");
$id = $_GET['id'];
// Ensure the ID is a valid integer to prevent SQL injection
$id = (int)$id;
// 1. Query to fetch the current image name
$sql = "SELECT Image FROM `slider` WHERE `id` = ?";
if ($stmt = mysqli_prepare($con, $sql)) {
mysqli_stmt_bind_param($stmt, "i", $id);
mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt, $old);
mysqli_stmt_fetch($stmt);
mysqli_stmt_close($stmt);
// If no image is found, exit
if (empty($old)) {
die("Image not found.");
}
// 2. Delete query
$sql1 = "DELETE FROM `slider` WHERE `id` = ?";
if ($stmt1 = mysqli_prepare($con, $sql1)) {
mysqli_stmt_bind_param($stmt1, "i", $id);
$query1 = mysqli_stmt_execute($stmt1);
mysqli_stmt_close($stmt1);
// If the query was successful, delete the image from the server
if ($query1) {
// Make sure the file exists before attempting to unlink it
$imagePath = "./assets/images/slider/" . $old;
if (file_exists($imagePath)) {
unlink($imagePath);
}
header("Location: viewslider.php");
exit;
} else {
echo "Error: Unable to delete record.";
}
} else {
echo "Error: Unable to prepare delete statement.";
}
} else {
echo "Error: Unable to prepare select statement.";
}
?>