prepare("SELECT password FROM users WHERE id = ?"); $stmt->bind_param("i", $userId); $stmt->execute(); $result = $stmt->get_result(); $userData = $result->fetch_assoc(); $stmt->close(); if (!$userData) { $_SESSION['error'] = "User not found."; header("Location: profile.php"); exit; } // Verify that the current password is correct. if (!password_verify($current_password, $userData['password'])) { $_SESSION['error'] = "Current password is incorrect."; header("Location: profile.php"); exit; } // Hash the new password. $hashed_new_password = password_hash($new_password, PASSWORD_DEFAULT); // Update the user's password in the database. $stmt = $db->prepare("UPDATE users SET password = ? WHERE id = ?"); $stmt->bind_param("si", $hashed_new_password, $userId); if (!$stmt->execute()) { $_SESSION['error'] = "Failed to update password. Please try again."; header("Location: profile.php"); exit; } $stmt->close(); $_SESSION['success'] = "Password updated successfully."; header("Location: profile.php"); exit;