prepare(" INSERT INTO recent_activities (title, description, image, activity_date, status, created_at) VALUES (?, ?, ?, ?, ?, NOW()) "); $stmt->execute([$title, $description, $image, $activity_date, $status]); $message = 'Activity successfully added!'; } else { // Keep existing image if no new image uploaded if (empty($image)) { $stmt = $db->prepare("SELECT image FROM recent_activities WHERE id = ?"); $stmt->execute([$id]); $existing = $stmt->fetch(); $image = $existing['image'] ?? ''; } $stmt = $db->prepare(" UPDATE recent_activities SET title = ?, description = ?, image = ?, activity_date = ?, status = ? WHERE id = ? "); $stmt->execute([$title, $description, $image, $activity_date, $status, $id]); $message = 'Activity updated successfully!'; } $action = 'list'; } catch (Exception $e) { $error = 'Error: ' . htmlspecialchars($e->getMessage()); } break; case 'delete': try { $delete_id = $_POST['id']; // Get image filename before deleting $stmt = $db->prepare("SELECT image FROM recent_activities WHERE id = ?"); $stmt->execute([$delete_id]); $activity = $stmt->fetch(); // Delete activity $stmt = $db->prepare("DELETE FROM recent_activities WHERE id = ?"); $stmt->execute([$delete_id]); // Delete image if it exists if (!empty($activity['image']) && file_exists('../img/activities/' . $activity['image'])) { unlink('../img/activities/' . $activity['image']); } $message = 'Activity successfully deleted!'; } catch (Exception $e) { $error = 'Deletion error: ' . htmlspecialchars($e->getMessage()); } break; } } } // Get activity data for editing $activity_data = null; if ($action === 'edit' && $id > 0) { $stmt = $db->prepare("SELECT * FROM recent_activities WHERE id = ?"); $stmt->execute([$id]); $activity_data = $stmt->fetch(); if (!$activity_data) { $error = 'Activity not found!'; $action = 'list'; } } // Get activities list $activities_list = []; if ($action === 'list') { $stmt = $db->prepare("SELECT * FROM recent_activities ORDER BY activity_date DESC"); $stmt->execute(); $activities_list = $stmt->fetchAll(); } include 'includes/header.php'; ?>