-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprofile.php
More file actions
210 lines (185 loc) · 6.45 KB
/
profile.php
File metadata and controls
210 lines (185 loc) · 6.45 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
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
<?php
// Start the session
session_start();
// Database connection
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "edtech";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Ensure the user is logged in
if (!isset($_SESSION['user_id'])) {
echo "<script>alert('You are not logged in. Please log in to update your profile.');</script>";
echo "<script>window.location.href = 'login.php';</script>";
exit;
}
$user_id = $_SESSION['user_id']; // Retrieve logged-in user ID from session
// Initialize variables to avoid undefined variable error
$fullname = "";
$email = "";
$profile_picture = "default.png"; // Default profile picture
// Fetch the user's current details from the database
$sql = "SELECT fullname, email, profile_picture FROM users WHERE id = ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param("i", $user_id);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows > 0) {
$user = $result->fetch_assoc();
$fullname = $user['fullname'];
$email = $user['email'];
$profile_picture = $user['profile_picture'];
} else {
echo "<script>alert('User not found. Please log in again.');</script>";
echo "<script>window.location.href = 'login.php';</script>";
exit;
}
$stmt->close();
// Check if the form is submitted
if ($_SERVER["REQUEST_METHOD"] === "POST") {
$fullname = trim($_POST['fullname']);
$email = trim($_POST['email']);
$profile_picture = $user['profile_picture']; // Keep the existing profile picture by default
// Handle file upload
if (!empty($_FILES['profile_picture']['name'])) {
$target_dir = "uploads/";
$profile_picture = $target_dir . basename($_FILES["profile_picture"]["name"]);
// Validate file type
$file_type = strtolower(pathinfo($profile_picture, PATHINFO_EXTENSION));
if (in_array($file_type, ['jpg', 'jpeg', 'png', 'gif'])) {
if (move_uploaded_file($_FILES["profile_picture"]["tmp_name"], $profile_picture)) {
// File uploaded successfully
} else {
echo "<script>alert('Error uploading file.');</script>";
}
} else {
echo "<script>alert('Invalid file type.');</script>";
$profile_picture = $user['profile_picture'];
}
}
// Update user in the database
$sql = "UPDATE users SET fullname = ?, email = ?, profile_picture = ? WHERE id = ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param("sssi", $fullname, $email, $profile_picture, $user_id);
if ($stmt->execute()) {
echo "<script>alert('Profile updated successfully!');</script>";
echo "<script>window.location.href = 'dashboard.php';</script>";
} else {
echo "<script>alert('Error: " . $stmt->error . "');</script>";
}
$stmt->close();
}
$conn->close();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Update Profile</title>
<link href="https://fonts.googleapis.com/css2?family=Roboto:wght@400;500;700&display=swap" rel="stylesheet">
<style>
body {
font-family: 'Roboto', sans-serif;
background-color: #f4f7fc;
margin: 0;
padding: 0;
}
.form-container {
background-color: #fff;
max-width: 500px;
margin: 50px auto;
padding: 30px;
border-radius: 8px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}
.form-container h2 {
text-align: center;
color: #333;
}
.form-group {
margin-bottom: 20px;
}
.form-group label {
display: block;
color: #555;
font-weight: 500;
margin-bottom: 5px;
}
.form-group input,
.form-group button {
width: 100%;
padding: 12px;
border-radius: 6px;
border: 1px solid #ccc;
font-size: 16px;
}
.form-group input:focus,
.form-group button:focus {
border-color: #4CAF50;
outline: none;
}
.form-group img {
max-width: 100px;
margin-top: 10px;
border-radius: 50%;
}
.form-group button {
background-color: rgb(7, 7, 67);
color: #fff;
border: none;
cursor: pointer;
transition: background-color 0.3s;
}
.form-group button:hover {
background-color: darkblue;
}
.form-group input[type="file"] {
padding: 5px;
cursor: pointer;
}
.alert {
padding: 10px;
background-color: #f44336;
color: white;
margin-bottom: 15px;
border-radius: 5px;
}
</style>
</head>
<body>
<div class="form-container">
<h2>Update Profile</h2>
<form action="" method="POST" enctype="multipart/form-data">
<div class="form-group">
<label for="fullname">Full Name:</label>
<input type="text" id="fullname" name="fullname" value="<?php echo htmlspecialchars($fullname); ?>" required>
</div>
<div class="form-group">
<label for="email">Email:</label>
<input type="email" id="email" name="email" value="<?php echo htmlspecialchars($email); ?>" required>
</div>
<div class="form-group">
<label for="profile_picture">Profile Picture:</label>
<input type="file" id="profile_picture" name="profile_picture" onchange="previewImage(event)">
<img src="<?php echo htmlspecialchars($profile_picture); ?>" alt="Profile Picture" id="profile_preview">
</div>
<div class="form-group">
<button type="submit">Update Profile</button>
</div>
</form>
</div>
<script>
function previewImage(event) {
const reader = new FileReader();
reader.onload = function() {
document.getElementById('profile_preview').src = reader.result;
};
reader.readAsDataURL(event.target.files[0]);
}
</script>
</body>
</html>