prepare(" INSERT INTO office_bearers (name, designation, photo, email, mobile, address, sort_order, status, created_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, NOW()) "); $stmt->execute([$name, $designation, $photo, $email, $mobile, $address, $sort_order, $status]); $message = 'कार्यालय पदाधिकारी सफलतापूर्वक जोड़ा गया!'; } else { // Keep existing photo if no new photo uploaded if (empty($photo)) { $stmt = $db->prepare("SELECT photo FROM office_bearers WHERE id = ?"); $stmt->execute([$id]); $existing = $stmt->fetch(); $photo = $existing['photo'] ?? ''; } $stmt = $db->prepare(" UPDATE office_bearers SET name = ?, designation = ?, photo = ?, email = ?, mobile = ?, address = ?, sort_order = ?, status = ? WHERE id = ? "); $stmt->execute([$name, $designation, $photo, $email, $mobile, $address, $sort_order, $status, $id]); $message = 'कार्यालय पदाधिकारी अपडेट किया गया!'; } $action = 'list'; } catch (Exception $e) { $error = 'त्रुटि: ' . $e->getMessage(); } break; case 'delete': try { $delete_id = $_POST['id']; // Get photo filename before deleting $stmt = $db->prepare("SELECT photo FROM office_bearers WHERE id = ?"); $stmt->execute([$delete_id]); $bearer = $stmt->fetch(); // Delete office bearer $stmt = $db->prepare("DELETE FROM office_bearers WHERE id = ?"); $stmt->execute([$delete_id]); // Delete photo if it exists if (!empty($bearer['photo']) && file_exists('../uploads/office-bearers/' . $bearer['photo'])) { unlink('../uploads/office-bearers/' . $bearer['photo']); } $message = 'कार्यालय पदाधिकारी सफलतापूर्वक हटाया गया!'; } catch (Exception $e) { $error = 'हटाने में त्रुटि: ' . $e->getMessage(); } break; } } } // Get office bearer data for editing $bearer_data = null; if ($action === 'edit' && $id > 0) { $stmt = $db->prepare("SELECT * FROM office_bearers WHERE id = ?"); $stmt->execute([$id]); $bearer_data = $stmt->fetch(); if (!$bearer_data) { $error = 'कार्यालय पदाधिकारी नहीं मिला!'; $action = 'list'; } } // Get office bearers list $office_bearers = []; if ($action === 'list') { $stmt = $db->prepare("SELECT * FROM office_bearers ORDER BY sort_order ASC, created_at DESC"); $stmt->execute(); $office_bearers = $stmt->fetchAll(); } include 'includes/header.php'; ?>