forked from IsmailSebz/UserManagementSystem
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathedit_profile.php
More file actions
139 lines (120 loc) · 4.9 KB
/
edit_profile.php
File metadata and controls
139 lines (120 loc) · 4.9 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
<?php
include_once 'includes/header.php';
requireLogin();
// Get user data
$userId = $_SESSION['user_id'];
$stmt = $conn->prepare("SELECT username, email, profile_picture FROM users WHERE id = ?");
$stmt->bind_param("i", $userId);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows === 1) {
$user = $result->fetch_assoc();
} else {
$_SESSION['message'] = "Error retrieving user data";
$_SESSION['message_type'] = "error";
header("Location: index.php");
exit();
}
// Handle form submission
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Get form data
$username = sanitizeInput($_POST['username']);
$email = sanitizeInput($_POST['email']);
$currentPassword = $_POST['current_password'];
$newPassword = $_POST['new_password'];
$confirmPassword = $_POST['confirm_password'];
// Validate form data
$errors = [];
if (empty($username)) {
$errors[] = "Username is required";
} elseif (strlen($username) < 3) {
$errors[] = "Username must be at least 3 characters";
}
if (empty($email)) {
$errors[] = "Email is required";
} elseif (!validateEmail($email)) {
$errors[] = "Invalid email format";
}
// Check if email is already used by another user
if ($email !== $user['email']) {
$stmt = $conn->prepare("SELECT id FROM users WHERE email = ? AND id != ?");
$stmt->bind_param("si", $email, $userId);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows > 0) {
$errors[] = "Email already in use by another account";
}
}
// Handle password change if provided
$updatePassword = false;
if (!empty($currentPassword) || !empty($newPassword) || !empty($confirmPassword)) {
if (empty($currentPassword)) {
$errors[] = "Current password is required to change password";
}
if (empty($newPassword)) {
$errors[] = "New password is required";
} elseif (strlen($newPassword) < 6) {
$errors[] = "New password must be at least 6 characters";
}
if ($newPassword !== $confirmPassword) {
$errors[] = "New passwords do not match";
}
// Verify current password
$stmt = $conn->prepare("SELECT password FROM users WHERE id = ?");
$stmt->bind_param("i", $userId);
$stmt->execute();
$result = $stmt->get_result();
$row = $result->fetch_assoc();
if (!password_verify($currentPassword, $row['password'])) {
$errors[] = "Current password is incorrect";
} else {
$updatePassword = true;
}
}
// Handle profile picture update if provided
$profilePicture = $user['profile_picture'];
if (isset($_FILES['profile_picture']) && $_FILES['profile_picture']['name'] !== '') {
$uploadResult = handleFileUpload($_FILES['profile_picture']);
if (is_array($uploadResult) && isset($uploadResult['error'])) {
$errors[] = $uploadResult['error'];
} else {
// Delete old profile picture if exists
if ($profilePicture) {
deleteProfilePicture($profilePicture);
}
$profilePicture = $uploadResult;
}
}
// If no errors, update user in database
if (empty($errors)) {
if ($updatePassword) {
// Hash new password
$hashedPassword = password_hash($newPassword, PASSWORD_DEFAULT);
// Update user with new password
$stmt = $conn->prepare("UPDATE users SET username = ?, email = ?, password = ?, profile_picture = ? WHERE id = ?");
$stmt->bind_param("ssssi", $username, $email, $hashedPassword, $profilePicture, $userId);
} else {
// Update user without changing password
$stmt = $conn->prepare("UPDATE users SET username = ?, email = ?, profile_picture = ? WHERE id = ?");
$stmt->bind_param("sssi", $username, $email, $profilePicture, $userId);
}
if ($stmt->execute()) {
// Update session username
$_SESSION['username'] = $username;
$_SESSION['message'] = "Profile updated successfully";
$_SESSION['message_type'] = "success";
header("Location: profile.php");
exit();
} else {
$errors[] = "Update failed. Please try again.";
// Restore old profile picture if update fails
if ($profilePicture !== $user['profile_picture']) {
deleteProfilePicture($profilePicture);
$profilePicture = $user['profile_picture'];
}
}
}
}
?>
<h2>Edit Profile</h2>
<div class="form-container