prepare("SELECT image_path FROM team_members WHERE id = :id"); $stmt_img->execute(['id' => $delete_id]); $row = $stmt_img->fetch(); if ($row && !empty($row['image_path']) && file_exists(__DIR__ . '/../' . $row['image_path'])) { // Keep sample images safe, only delete custom ones if (strpos($row['image_path'], 'team-') === false) { @unlink(__DIR__ . '/../' . $row['image_path']); } } $stmt = $pdo->prepare("DELETE FROM team_members WHERE id = :id"); $stmt->execute(['id' => $delete_id]); header("Location: " . BASE_URL . "admin/team.php?success=deleted"); exit(); } catch (PDOException $e) { $error_msg = "Failed to delete team member: " . $e->getMessage(); } } // Handle Form Submission if (isset($_POST['submit_team'])) { $name = trim($_POST['name']); $role = trim($_POST['role']); $bio = trim($_POST['bio']); if (empty($name) || empty($role)) { $error_msg = "Name and Designation are required fields."; } else { try { if ($action === 'add') { $img_path = handle_team_upload($_FILES['image']); if (!$img_path) $img_path = 'assets/images/team-1.jpg'; // fallback default $stmt = $pdo->prepare("INSERT INTO team_members (name, role, image_path, bio) VALUES (:name, :role, :image_path, :bio)"); $stmt->execute([ 'name' => $name, 'role' => $role, 'image_path' => $img_path, 'bio' => $bio ]); header("Location: " . BASE_URL . "admin/team.php?success=added"); exit(); } elseif ($action === 'edit') { $stmt_orig = $pdo->prepare("SELECT image_path FROM team_members WHERE id = :id"); $stmt_orig->execute(['id' => $id]); $orig_path = $stmt_orig->fetchColumn(); $new_path = handle_team_upload($_FILES['image']); if ($new_path) { if (!empty($orig_path) && file_exists(__DIR__ . '/../' . $orig_path) && strpos($orig_path, 'team-') === false) { @unlink(__DIR__ . '/../' . $orig_path); } $orig_path = $new_path; } $stmt = $pdo->prepare("UPDATE team_members SET name = :name, role = :role, image_path = :image_path, bio = :bio WHERE id = :id"); $stmt->execute([ 'name' => $name, 'role' => $role, 'image_path' => $orig_path, 'bio' => $bio, 'id' => $id ]); header("Location: " . BASE_URL . "admin/team.php?success=updated"); exit(); } } catch (PDOException $e) { $error_msg = "Error updating database: " . $e->getMessage(); } } } // Edit Mode detail pre-filler $edit_data = ['name' => '', 'role' => '', 'image_path' => '', 'bio' => '']; if ($action === 'edit' && $id > 0) { try { $stmt = $pdo->prepare("SELECT * FROM team_members WHERE id = :id"); $stmt->execute(['id' => $id]); $edit_data = $stmt->fetch(); if (!$edit_data) { header("Location: " . BASE_URL . "admin/team.php"); exit(); } } catch (PDOException $e) { $error_msg = "Error loading database row: " . $e->getMessage(); } } // Load all team members $all_team = []; try { $stmt = $pdo->query("SELECT * FROM team_members ORDER BY id ASC"); $all_team = $stmt->fetchAll(); } catch (PDOException $e) { // offline mock } require_once __DIR__ . '/header.php'; require_once __DIR__ . '/navbar.php'; ?>