-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprofile.php
More file actions
151 lines (138 loc) · 4.68 KB
/
Copy pathprofile.php
File metadata and controls
151 lines (138 loc) · 4.68 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<?php
session_start();
// Check if the user is logged in
if (!isset($_SESSION['user_id'])) {
header("Location: login.php");
exit();
}
include 'db.php';
// Fetch current user details from the database
$user_id = $_SESSION['user_id'];
$sql = "SELECT username, email FROM user WHERE id = ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param("i", $user_id);
$stmt->execute();
$result = $stmt->get_result();
$user = $result->fetch_assoc();
// Update profile logic
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$new_username = $_POST['username'];
$new_email = $_POST['email'];
$new_password = $_POST['password'];
// If password is provided, hash it and update, otherwise just update username and email
if (!empty($new_password)) {
$hashed_password = password_hash($new_password, PASSWORD_DEFAULT);
$update_sql = "UPDATE user SET username = ?, email = ?, password = ? WHERE id = ?";
$stmt = $conn->prepare($update_sql);
$stmt->bind_param("sssi", $new_username, $new_email, $hashed_password, $user_id);
} else {
$update_sql = "UPDATE user SET username = ?, email = ? WHERE id = ?";
$stmt = $conn->prepare($update_sql);
$stmt->bind_param("ssi", $new_username, $new_email, $user_id);
}
if ($stmt->execute()) {
$_SESSION['success'] = "Profile updated successfully!";
header("Location: profile.php");
exit();
} else {
$_SESSION['error'] = "An error occurred. Please try again.";
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Profile</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css">
<style>
body {
background-color: #f8f9fa;
}
.container {
max-width: 900px;
margin-top: 50px;
background: white;
padding: 30px;
border-radius: 10px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}
h2 {
text-align: center;
color: #343a40;
}
table {
margin-top: 20px;
}
.btn-remove {
color: white;
background-color: #dc3545;
padding: 5px 10px;
text-decoration: none;
border-radius: 5px;
}
.btn-remove:hover {
background-color: #c82333;
}
.total-price {
text-align: right;
font-size: 1.2rem;
font-weight: bold;
}
.form-control {
border-radius: 10px;
}
.form-label {
font-weight: 600;
}
.btn-primary {
background-color: #007bff;
border-color: #007bff;
border-radius: 10px;
}
.btn-primary:hover {
background-color: #0056b3;
border-color: #0056b3;
}
</style>
</head>
<body>
<div class="container">
<h2>👤 Your Profile</h2>
<!-- Display success or error messages -->
<?php if (isset($_SESSION['success'])): ?>
<div class="alert alert-success text-center mt-4">
✅ <?php echo $_SESSION['success']; ?>
</div>
<?php unset($_SESSION['success']); ?>
<?php endif; ?>
<?php if (isset($_SESSION['error'])): ?>
<div class="alert alert-danger text-center mt-4">
❌ <?php echo $_SESSION['error']; ?>
</div>
<?php unset($_SESSION['error']); ?>
<?php endif; ?>
<!-- Profile form -->
<form method="POST" action="profile.php">
<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</label>
<input type="email" class="form-control" id="email" name="email" value="<?php echo htmlspecialchars($user['email']); ?>" required>
</div>
<div class="mb-3">
<label for="password" class="form-label">New Password (Optional)</label>
<input type="password" class="form-control" id="password" name="password" placeholder="Leave blank to keep current password">
</div>
<div class="text-center">
<button type="submit" class="btn btn-primary btn-lg w-100">Update Profile</button>
</div>
</form>
</div>
<!-- Bootstrap JS (Optional) -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>