72 lines
2.2 KiB
PHP
72 lines
2.2 KiB
PHP
<?php
|
|
require_once 'includes/header.php';
|
|
require_once 'db/config.php';
|
|
|
|
if (!isset($_SESSION['user_id'])) {
|
|
header('Location: login.php');
|
|
exit;
|
|
}
|
|
|
|
$user_id = $_SESSION['user_id'];
|
|
|
|
$message = '';
|
|
$error = '';
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$username = $_POST['username'];
|
|
$email = $_POST['email'];
|
|
|
|
if (empty($username) || empty($email)) {
|
|
$error = 'Username and email are required.';
|
|
} else {
|
|
try {
|
|
$stmt = db()->prepare("UPDATE users SET username = ?, email = ? WHERE id = ?");
|
|
$stmt->execute([$username, $email, $user_id]);
|
|
$_SESSION['user_name'] = $username;
|
|
$message = 'Profile updated successfully!';
|
|
} catch (PDOException $e) {
|
|
$error = 'Error updating profile: ' . $e->getMessage();
|
|
}
|
|
}
|
|
}
|
|
|
|
$stmt = db()->prepare("SELECT * FROM users WHERE id = ?");
|
|
$stmt->execute([$user_id]);
|
|
$user = $stmt->fetch();
|
|
|
|
?>
|
|
|
|
<div class="container mt-5">
|
|
<h1>User Profile</h1>
|
|
|
|
<?php if ($message): ?>
|
|
<div class="alert alert-success">
|
|
<?php echo $message; ?>
|
|
</div>
|
|
<?php endif; ?>
|
|
|
|
<?php if ($error): ?>
|
|
<div class="alert alert-danger">
|
|
<?php echo $error; ?>
|
|
</div>
|
|
<?php endif; ?>
|
|
|
|
<div class="card">
|
|
<div class="card-body">
|
|
<h5 class="card-title">Edit Your Information</h5>
|
|
<form action="profile.php" method="POST">
|
|
<div class="mb-3">
|
|
<label for="username" class="form-label">Username</label>
|
|
<input type="text" class="form-control" id="username" name="username" value="<?php echo htmlspecialchars($user['username']); ?>" 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($user['email']); ?>" required>
|
|
</div>
|
|
<button type="submit" class="btn btn-primary">Update Profile</button>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<?php require_once 'includes/footer.php'; ?>
|