How do I make my dynamic dropdown sticky?

I have a dropdown menu in a form that is dynamic and pulls in the options from a database. It works great. The only problem is if there is an error in the form when it is submitted all of the values that were entered are cleared. How can I make my drop down menu sticky so that it keeps the values that were entered when an error occurs upon submission of the form?

Here is the code for my drop down:


<div class="field">
      <label for="machinenum">Machine #</label>
      <select name="machinenum">
      <option value="null">Choose One</option>


      $conn = mysql_connect("localhost","user","pass") or trigger_error("SQL", E_USER_ERROR);
      $db = mysql_select_db("database", $conn) or trigger_error("SQL", E_USER_ERROR);
            
      $sql_machinenum = "SELECT * FROM machinenum ORDER BY machinenum_drop ASC";
      $result = mysql_query($sql_machinenum, $conn) or trigger_error("SQL", E_USER_ERROR);
                                        
      while($row = mysql_fetch_assoc($result)) {
            $selection = $row['machinenum_drop'];
            $machinenum_tbl = $row['machinenum'];
            if ($row['machinenum_drop'] == $machinenum_tbl){
                   echo "<option value=\\"$selection\\" SELECTED>$selection</option>\
";
            }else{
                    echo "<option value=\\"$selection\\">$selection</option>\
";
            }
      }
       mysql_close($db);


       </select>
</div>

Maybe this will work:



$machinenum_tbl="null";
if (isset($_POST['machinenum'])) {
      $machinenum_tbl=$_POST['machinenum'];
}

$conn = mysql_connect("localhost","user","pass") or trigger_error("SQL", E_USER_ERROR); 
      $db = mysql_select_db("database", $conn) or trigger_error("SQL", E_USER_ERROR); 
             
      $sql_machinenum = "SELECT * FROM machinenum ORDER BY machinenum_drop ASC"; 
      $result = mysql_query($sql_machinenum, $conn) or trigger_error("SQL", E_USER_ERROR); 
                                         
      while($row = mysql_fetch_assoc($result)) { 
            $selection = $row['machinenum_drop']; 
            if ($selection == $machinenum_tbl){ 
                   echo "<option value=\\"$selection\\" SELECTED>$selection</option>\
"; 
            }else{ 
                    echo "<option value=\\"$selection\\">$selection</option>\
"; 
            } 
      } 
       mysql_close($db); 


Idea: storing the form value submitted by the user and using that one as the default or using null if the form was not submitted.

That works great. Thanks!

Oops! I spoke too soon. It works, but it messes up my form. My form has an input for an MO #. When the user inputs an MO # such as 1 and clicks the Fill in Data Button the next 7 fields are populated from the database. When I add the code above it disables that functionality. It works for making the field sticky but disables the ability to populate the field. How do I keep the ability to populate those fields and make the fields sticky?
Here is a link to the form: http://www.mccawphotographics.com/qc/index-sticky-forum.php