if it is not using a value from the recordset for the folder name it suggests that the recordset is empty.
you don't provide the full page, so it is difficult to say for certain, but my guess is that the contest querystring variable is being passed into the page when it initially loads, but is not passed by the forms action when the form is submitted
use the following code for the action of the form so that any querystring variables passed in initialy will also be included in the form post:
action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?><?php echo (isset($_SERVER["QUERY_STRING"]) && $_SERVER["QUERY_STRING"] != "")?"?".htmlspecialchars($_SERVER["QUERY_STRING"]):""; ?>