prepare($query); $stmt->bind_param("i", $user_id); $stmt->execute(); $result = $stmt->get_result(); if ($result->num_rows === 1) { $user = $result->fetch_assoc(); } else { $error = "User not found."; } // Process profile update form if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['update_profile'])) { $email = $conn->real_escape_string(trim($_POST['email'])); // Validate email if (empty($email)) { $error = "Email is required."; } elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) { $error = "Please enter a valid email address."; } else { // Check if email is already used by another user $check_query = "SELECT id FROM users WHERE email = ? AND id != ?"; $check_stmt = $conn->prepare($check_query); $check_stmt->bind_param("si", $email, $user_id); $check_stmt->execute(); $check_result = $check_stmt->get_result(); if ($check_result->num_rows > 0) { $error = "Email is already in use by another account."; } else { // Update user profile $update_query = "UPDATE users SET email = ? WHERE id = ?"; $update_stmt = $conn->prepare($update_query); $update_stmt->bind_param("si", $email, $user_id); if ($update_stmt->execute()) { $_SESSION['email'] = $email; $success = "Profile updated successfully."; // Refresh user data $stmt->execute(); $result = $stmt->get_result(); $user = $result->fetch_assoc(); } else { $error = "Failed to update profile: " . $conn->error; } } } } // Process password change form if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['change_password'])) { $current_password = trim($_POST['current_password']); $new_password = trim($_POST['new_password']); $confirm_password = trim($_POST['confirm_password']); // Validate inputs if (empty($current_password) || empty($new_password) || empty($confirm_password)) { $error = "All password fields are required."; } elseif (strlen($new_password) < 6) { $error = "New password must be at least 6 characters long."; } elseif ($new_password !== $confirm_password) { $error = "New passwords do not match."; } elseif (!password_verify($current_password, $user['password'])) { $error = "Current password is incorrect."; } else { // Update password $hashed_password = password_hash($new_password, PASSWORD_DEFAULT); $update_query = "UPDATE users SET password = ? WHERE id = ?"; $update_stmt = $conn->prepare($update_query); $update_stmt->bind_param("si", $hashed_password, $user_id); if ($update_stmt->execute()) { $success = "Password changed successfully."; } else { $error = "Failed to change password: " . $conn->error; } } } // Include header include 'includes/header.php'; ?>
Manage your account settings and password