-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprofile.php
184 lines (172 loc) · 6.66 KB
/
profile.php
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
<?php
session_start();
if (!isset($_SESSION['user_id'])) {
header("Location: login.php");
exit();
}
// Database connection
$host = 'localhost';
$db = 'nourishunity';
$user = 'root';
$pass = '';
try {
$conn = new PDO("mysql:host=$host;dbname=$db", $user, $pass);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
die("Database connection failed: " . $e->getMessage());
}
echo ("Database connection");
// Fetch user details
$userId = $_SESSION['user_id'];
$sql = "SELECT name, email, role, profile_image FROM users WHERE id = :id";
$stmt = $conn->prepare($sql);
$stmt->execute(['id' => $userId]);
$user = $stmt->fetch(PDO::FETCH_ASSOC);
// Handle form submission
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$name = htmlspecialchars($_POST['name']);
$password = !empty($_POST['password']) ? password_hash($_POST['password'], PASSWORD_DEFAULT) : null;
$profileImage = $user['profile_image'];
// Handle profile image upload
if (isset($_FILES['profile_image']) && $_FILES['profile_image']['error'] == 0) {
$targetDir = "uploads/profiles/";
$targetFile = $targetDir . basename($_FILES['profile_image']['name']);
if (move_uploaded_file($_FILES['profile_image']['tmp_name'], $targetFile)) {
$profileImage = $targetFile;
}
}
// Update user details
if ($password) {
$updateSql = "UPDATE users SET name = :name, password = :password, profile_image = :profile_image WHERE id = :id";
$stmt = $conn->prepare($updateSql);
$stmt->execute(['name' => $name, 'password' => $password, 'profile_image' => $profileImage, 'id' => $userId]);
} else {
$updateSql = "UPDATE users SET name = :name, profile_image = :profile_image WHERE id = :id";
$stmt = $conn->prepare($updateSql);
$stmt->execute(['name' => $name, 'profile_image' => $profileImage, 'id' => $userId]);
}
$_SESSION['name'] = $name; // Update session with new name
echo "<script>alert('Profile updated successfully!'); window.location.href='profile.php';</script>";
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>My Profile</title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
<style>
body {
font-family: 'Arial', sans-serif;
background: #f4f7fa;
}
.profile-container {
max-width: 800px;
margin: 50px auto;
padding: 30px;
background-color: white;
border-radius: 10px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}
.profile-header {
display: flex;
align-items: center;
margin-bottom: 30px;
}
.profile-img {
width: 120px;
height: 120px;
border-radius: 50%;
object-fit: cover;
margin-right: 20px;
border: 3px solid #007bff;
}
.profile-header h2 {
margin: 0;
color: #007bff;
}
.role-badge {
font-size: 1rem;
margin-top: 5px;
color: white;
background-color: #28a745;
border-radius: 15px;
padding: 5px 10px;
display: inline-block;
}
.custom-file-upload {
background-color: #007bff;
border: none;
color: white;
padding: 10px 20px;
font-size: 1rem;
cursor: pointer;
border-radius: 5px;
text-align: center;
}
.custom-file-upload:hover {
background-color: #0056b3;
}
.input-file {
display: none;
}
.btn-primary {
background-color: #007bff;
border-color: #007bff;
}
.btn-primary:hover {
background-color: #0056b3;
}
</style>
</head>
<body>
<div class="profile-container">
<!-- Profile Header -->
<div class="profile-header">
<img id="profile_img_preview" src="<?php echo $user['profile_image'] ?: 'uploads/profiles/default-profile.png'; ?>" alt="Profile Picture" class="profile-img">
<div>
<h2><?php echo htmlspecialchars($user['name']); ?></h2>
<span class="role-badge"><?php echo ucfirst($user['role']); ?></span>
</div>
</div>
<!-- Profile Form -->
<form action="profile.php" method="POST" enctype="multipart/form-data">
<!-- Custom Profile Picture Upload -->
<div class="mb-3">
<label class="form-label">Profile Picture</label>
<label for="profile_image" class="custom-file-upload">
Choose File
</label>
<input type="file" class="input-file" id="profile_image" name="profile_image" accept="image/*" onchange="previewImage(event)">
</div>
<!-- Name -->
<div class="mb-3">
<label for="name" class="form-label">Name</label>
<input type="text" class="form-control" id="name" name="name" value="<?php echo htmlspecialchars($user['name']); ?>" required>
</div>
<!-- Email -->
<div class="mb-3">
<label for="email" class="form-label">Email</label>
<input type="email" class="form-control" id="email" value="<?php echo htmlspecialchars($user['email']); ?>" readonly>
</div>
<!-- Password -->
<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="Enter a new password">
</div>
<button type="submit" class="btn btn-primary w-100">Update Profile</button>
</form>
</div>
<script>
// Preview the uploaded profile image
function previewImage(event) {
var reader = new FileReader();
reader.onload = function() {
var output = document.getElementById('profile_img_preview');
output.src = reader.result; // Set the uploaded image as source for the preview
};
reader.readAsDataURL(event.target.files[0]); // Read the file as data URL
}
</script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>