36694-vm/edit_donor_profile.php
Flatlogic Bot c12628e2d9 v2
2025-12-05 21:07:12 +00:00

145 lines
7.5 KiB
PHP

<?php
session_start();
require_once 'db/config.php';
// Check if user is logged in and is a donor
if (!isset($_SESSION['user_id']) || $_SESSION['user_type'] !== 'donor') {
header("Location: login.php");
exit;
}
$page_title = "Edit Profile";
$donor_id = $_SESSION['user_id'];
$error_message = '';
$success_message = '';
$pdo = db();
// Fetch donor data
$stmt = $pdo->prepare("SELECT * FROM donors WHERE id = ?");
$stmt->execute([$donor_id]);
$donor = $stmt->fetch(PDO::FETCH_ASSOC);
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = trim($_POST['name']);
$email = trim($_POST['email']);
$phone = trim($_POST['phone']);
$blood_type = trim($_POST['blood_type']);
$organs = isset($_POST['organs']) ? implode(',', $_POST['organs']) : '';
$medical_history = trim($_POST['medical_history']);
if (empty($name) || empty($email) || empty($blood_type)) {
$error_message = "Please fill in all required fields.";
} else {
try {
$sql = "UPDATE donors SET name = ?, email = ?, phone = ?, blood_type = ?, organs = ?, medical_history = ? WHERE id = ?";
$stmt = $pdo->prepare($sql);
$stmt->execute([$name, $email, $phone, $blood_type, $organs, $medical_history, $donor_id]);
$success_message = "Profile updated successfully!";
// Refresh donor data
$stmt = $pdo->prepare("SELECT * FROM donors WHERE id = ?");
$stmt->execute([$donor_id]);
$donor = $stmt->fetch(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
$error_message = "Database error: " . $e->getMessage();
}
}
}
$organs_available = ['Heart', 'Lungs', 'Liver', 'Kidneys', 'Pancreas', 'Intestines', 'Corneas'];
$donor_organs = explode(',', $donor['organs']);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title><?= htmlspecialchars($page_title) ?> - Organ Donation</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="assets/css/custom.css">
</head>
<body>
<header class="header bg-primary text-white text-center py-4">
<div class="container">
<h1 class="display-4">Organ Donation Management</h1>
<nav class="nav justify-content-center">
<a class="nav-link text-white" href="donor_dashboard.php">Dashboard</a>
<a class="nav-link text-white active" href="edit_donor_profile.php">Edit Profile</a>
<a class="nav-link text-white" href="logout.php">Logout</a>
</nav>
</div>
</header>
<main class="container my-5">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header bg-primary text-white">
<h2 class="h4 mb-0">Edit Your Profile</h2>
</div>
<div class="card-body">
<?php if ($success_message): ?>
<div class="alert alert-success"><?= htmlspecialchars($success_message) ?></div>
<?php endif; ?>
<?php if ($error_message): ?>
<div class="alert alert-danger"><?= htmlspecialchars($error_message) ?></div>
<?php endif; ?>
<form action="edit_donor_profile.php" method="POST">
<div class="mb-3">
<label for="name" class="form-label">Full Name</label>
<input type="text" class="form-control" id="name" name="name" value="<?= htmlspecialchars($donor['name']) ?>" required>
</div>
<div class="mb-3">
<label for="email" class="form-label">Email</label>
<input type="email" class="form-control" id="email" name="email" value="<?= htmlspecialchars($donor['email']) ?>" required>
</div>
<div class="mb-3">
<label for="phone" class="form-label">Phone</label>
<input type="tel" class="form-control" id="phone" name="phone" value="<?= htmlspecialchars($donor['phone']) ?>">
</div>
<div class="mb-3">
<label for="blood_type" class="form-label">Blood Type</label>
<select class="form-select" id="blood_type" name="blood_type" required>
<option value="A+" <?= $donor['blood_type'] == 'A+' ? 'selected' : '' ?>>A+</option>
<option value="A-" <?= $donor['blood_type'] == 'A-' ? 'selected' : '' ?>>A-</option>
<option value="B+" <?= $donor['blood_type'] == 'B+' ? 'selected' : '' ?>>B+</option>
<option value="B-" <?= $donor['blood_type'] == 'B-' ? 'selected' : '' ?>>B-</option>
<option value="AB+" <?= $donor['blood_type'] == 'AB+' ? 'selected' : '' ?>>AB+</option>
<option value="AB-" <?= $donor['blood_type'] == 'AB-' ? 'selected' : '' ?>>AB-</option>
<option value="O+" <?= $donor['blood_type'] == 'O+' ? 'selected' : '' ?>>O+</option>
<option value="O-" <?= $donor['blood_type'] == 'O-' ? 'selected' : '' ?>>O-</option>
</select>
</div>
<div class="mb-3">
<label class="form-label">Organs to Donate</label>
<div>
<?php foreach ($organs_available as $organ): ?>
<div class="form-check form-check-inline">
<input class="form-check-input" type="checkbox" name="organs[]" value="<?= $organ ?>" id="organ_<?= strtolower($organ) ?>" <?= in_array($organ, $donor_organs) ? 'checked' : '' ?>>
<label class="form-check-label" for="organ_<?= strtolower($organ) ?>"><?= $organ ?></label>
</div>
<?php endforeach; ?>
</div>
</div>
<div class="mb-3">
<label for="medical_history" class="form-label">Medical History / Conditions</label>
<textarea class="form-control" id="medical_history" name="medical_history" rows="4"><?= htmlspecialchars($donor['medical_history']) ?></textarea>
</div>
<button type="submit" class="btn btn-primary">Save Changes</button>
</form>
</div>
</div>
</div>
</div>
</main>
<footer class="footer bg-light text-center py-3 mt-5">
<div class="container">
<p class="mb-0">&copy; <?= date("Y") ?> Organ Donation Management System. All Rights Reserved.</p>
</div>
</footer>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>