-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprofile.php
More file actions
64 lines (60 loc) · 2.13 KB
/
profile.php
File metadata and controls
64 lines (60 loc) · 2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php
session_start();
include "db.php"; // Include your database connection
if (!isset($_SESSION["user_id"])) {
header("Location: login.php");
exit();
}
// Fetch user details
$user_id = $_SESSION["user_id"];
$stmt = $conn->prepare("SELECT name, email FROM users WHERE id = ?");
$stmt->bind_param("i", $user_id);
$stmt->execute();
$result = $stmt->get_result();
$user = $result->fetch_assoc();
// Handle updating user details
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['update_profile'])) {
$name = trim($_POST["name"]);
$email = trim($_POST["email"]);
$stmt = $conn->prepare("UPDATE users SET name = ?, email = ? WHERE id = ?");
$stmt->bind_param("ssi", $name, $email, $user_id);
if ($stmt->execute()) {
$_SESSION["user_name"] = $name; // Update session variable
$success = "Profile updated successfully!";
} else {
$error = "Error updating profile.";
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>User Profile</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container mt-5">
<h2>User Profile</h2>
<?php if (isset($success)): ?>
<div class="alert alert-success"><?= htmlspecialchars($success); ?></div>
<?php endif; ?>
<?php if (isset($error)): ?>
<div class="alert alert-danger"><?= htmlspecialchars($error); ?></div>
<?php endif; ?>
<form method="post">
<div class="mb-3">
<label for="name" class="form-label">Name</label>
<input type="text" name="name" class="form-control" value="<?= htmlspecialchars($user['name']); ?>" required>
</div>
<div class="mb-3">
<label for="email" class="form-label">Email</label>
<input type="email" name="email" class="form-control" value="<?= htmlspecialchars($user['email']); ?>" required>
</div>
<button type="submit" name="update_profile" class="btn btn-primary">Update Profile</button>
</form>
<a href="logout.php" class="btn btn-danger mt-3">Logout</a>
</div>
</body>
</html>
?>