Gallery Management
Gallery Images ()
No images in gallery yet. Click "Add New Images" to upload.
prepare("INSERT INTO gallery (image) VALUES (?)"); $stmt->execute([$image]); $uploaded_count++; } } } } // Redirect to prevent form resubmission header("Location: ?success=add&count=" . $uploaded_count); exit; } catch (Exception $e) { $error = 'Error: ' . $e->getMessage(); } } elseif (isset($_POST['action']) && $_POST['action'] === 'delete' && isset($_POST['id'])) { try { $delete_id = intval($_POST['id']); // Verify the ID is valid if ($delete_id <= 0) { throw new Exception('Invalid image ID'); } // Get image filename before deleting $stmt = $db->prepare("SELECT image FROM gallery WHERE id = ?"); $stmt->execute([$delete_id]); $gallery = $stmt->fetch(); if ($gallery) { // Delete gallery item from database $stmt = $db->prepare("DELETE FROM gallery WHERE id = ?"); $stmt->execute([$delete_id]); // Delete physical file if it exists if (!empty($gallery['image']) && file_exists('../img/gallery/' . $gallery['image'])) { unlink('../img/gallery/' . $gallery['image']); } // Redirect to prevent form resubmission header("Location: ?success=delete"); exit; } else { throw new Exception('Image not found'); } } catch (Exception $e) { $error = 'Error deleting image: ' . $e->getMessage(); } } } // Handle success messages from redirects if (isset($_GET['success'])) { if ($_GET['success'] === 'add') { $count = isset($_GET['count']) ? intval($_GET['count']) : 0; $message = $count . ' image(s) added successfully!'; } elseif ($_GET['success'] === 'delete') { $message = 'Image deleted successfully!'; } } // Get gallery list $gallery_list = []; if ($action === 'list') { $stmt = $db->prepare("SELECT * FROM gallery ORDER BY id DESC"); $stmt->execute(); $gallery_list = $stmt->fetchAll(); } include 'includes/header.php'; ?>
No images in gallery yet. Click "Add New Images" to upload.