prepare("SELECT id FROM users WHERE username = ? AND id != ?"); $stmt->bind_param("si", $new_username, $userId); $stmt->execute(); $result = $stmt->get_result(); if ($result->num_rows > 0) { $_SESSION['error'] = "Username already taken."; header("Location: profile.php"); exit; } $stmt->close(); // Update the username in the database $stmt = $db->prepare("UPDATE users SET username = ? WHERE id = ?"); $stmt->bind_param("si", $new_username, $userId); $stmt->execute(); $stmt->close(); // Update session data $_SESSION['user']['username'] = $new_username; $_SESSION['success'] = "Username updated successfully."; header("Location: profile.php"); exit;