I want if the user clicks submit to return the the selected value when the form reloads

that what I try and no luck

<?php
            $getData = $db->prepare("SELECT * FROM first_university_degree_list");
            if ($getData->execute()) {
                $res = $getData->get_result();
                while ($data = $res->fetch_array()) {
                    ?>
                    <option value="<?php echo $data['univ_degree']; ?>" <?php if($FirstUniversityDegree == $data['univ_degree']) echo 'selected="selected"'; ?>><?php echo $data['univ_degree']; ?></option>
                <?php
                }
            }
            ?>

clearing the question
I have a validation for every field on the form so if any of this fields return false the options on the drop menu will return to the first item I need it to return on the selected item the users selected

the id and name of the select field are missing from the code sample, I have chosen 'fieldname'

<?php if($_POST['fieldname']){ 
echo "<option value='$_POST['fieldname'] selected='selected'> $_POST['fieldname'] </option>";}
else { 
$getData = $db->prepare("SELECT * FROM first_university_degree_list");
 if ($getData->execute()) {
  $res = $getData->get_result();
  while ($data = $res->fetch_array()) {
   echo "<option value='$data['univ_degree']'";   
   if($FirstUniversityDegree == $data['univ_degree']) {echo 'selected="selected"';} 
   echo "$data['univ_degree'] </option>";
  }
 }
} ?>

If the data is posted, use that, else run the select statement

syntax may be incorrect due to not seeing the entire code page, may be _GET etc, but should provide a base for dev

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.