131 lines
5.4 KiB
PHP
131 lines
5.4 KiB
PHP
<?php
|
|
session_start();
|
|
if (!isset($_SESSION['farmer_id'])) {
|
|
header('Location: login.php');
|
|
exit;
|
|
}
|
|
|
|
require_once 'db/config.php';
|
|
|
|
$farmer_id = $_SESSION['farmer_id'];
|
|
$errors = [];
|
|
$message = '';
|
|
|
|
// Fetch farmer data
|
|
try {
|
|
$stmt = db()->prepare("SELECT * FROM farmers WHERE id = ?");
|
|
$stmt->execute([$farmer_id]);
|
|
$farmer = $stmt->fetch(PDO::FETCH_ASSOC);
|
|
} catch (PDOException $e) {
|
|
die("Database error: " . $e->getMessage());
|
|
}
|
|
|
|
$full_name = $farmer['full_name'];
|
|
$email = $farmer['email'];
|
|
$phone = $farmer['phone'];
|
|
$district = $farmer['district'];
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$full_name = trim($_POST['full_name'] ?? '');
|
|
$email = trim($_POST['email'] ?? '');
|
|
$phone = trim($_POST['phone'] ?? '');
|
|
$district = trim($_POST['district'] ?? '');
|
|
|
|
if (empty($full_name)) {
|
|
$errors[] = 'Full name is required.';
|
|
}
|
|
if (empty($email)) {
|
|
$errors[] = 'Email is required.';
|
|
} elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
|
|
$errors[] = 'Invalid email format.';
|
|
}
|
|
|
|
// Check if email is already taken by another user
|
|
$stmt = db()->prepare("SELECT id FROM farmers WHERE email = ? AND id != ?");
|
|
$stmt->execute([$email, $farmer_id]);
|
|
if ($stmt->fetch()) {
|
|
$errors[] = 'This email address is already in use by another account.';
|
|
}
|
|
|
|
if (empty($errors)) {
|
|
try {
|
|
$stmt = db()->prepare("UPDATE farmers SET full_name = ?, email = ?, phone = ?, district = ? WHERE id = ?");
|
|
$stmt->execute([$full_name, $email, $phone, $district, $farmer_id]);
|
|
$message = 'Profile updated successfully!';
|
|
// Re-fetch data to display updated values
|
|
$full_name = $full_name;
|
|
$email = $email;
|
|
$phone = $phone;
|
|
$district = $district;
|
|
} catch (PDOException $e) {
|
|
$errors[] = 'Database error: ' . $e->getMessage();
|
|
}
|
|
}
|
|
}
|
|
|
|
?>
|
|
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>Edit Profile - Smart Farmer</title>
|
|
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
|
|
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css">
|
|
<link rel="stylesheet" href="assets/css/custom.css">
|
|
</head>
|
|
<body>
|
|
<?php include 'partials/navbar.php'; ?>
|
|
|
|
<div class="container mt-5">
|
|
<div class="row justify-content-center">
|
|
<div class="col-md-8">
|
|
<div class="card shadow-sm">
|
|
<div class="card-header bg-primary text-white">
|
|
<h1 class="h3 mb-0">Edit Your Profile</h1>
|
|
</div>
|
|
<div class="card-body">
|
|
<?php if (!empty($message)): ?>
|
|
<div class="alert alert-success"><?php echo htmlspecialchars($message); ?></div>
|
|
<?php endif; ?>
|
|
<?php if (!empty($errors)): ?>
|
|
<div class="alert alert-danger">
|
|
<ul>
|
|
<?php foreach ($errors as $error): ?>
|
|
<li><?php echo htmlspecialchars($error); ?></li>
|
|
<?php endforeach; ?>
|
|
</ul>
|
|
</div>
|
|
<?php endif; ?>
|
|
|
|
<form action="edit_profile.php" method="POST">
|
|
<div class="mb-3">
|
|
<label for="full_name" class="form-label">Full Name</label>
|
|
<input type="text" class="form-control" id="full_name" name="full_name" value="<?php echo htmlspecialchars($full_name); ?>" required>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="email" class="form-label">Email Address</label>
|
|
<input type="email" class="form-control" id="email" name="email" value="<?php echo htmlspecialchars($email); ?>" required>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="phone" class="form-label">Phone Number</label>
|
|
<input type="tel" class="form-control" id="phone" name="phone" value="<?php echo htmlspecialchars($phone); ?>">
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="district" class="form-label">District</label>
|
|
<input type="text" class="form-control" id="district" name="district" value="<?php echo htmlspecialchars($district); ?>">
|
|
</div>
|
|
<div class="d-flex justify-content-between">
|
|
<button type="submit" class="btn btn-primary">Update Profile</button>
|
|
<a href="dashboard.php" class="btn btn-secondary">Back to Dashboard</a>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
|
|
</body>
|
|
</html>
|