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