title: Form isn't showing select dropdown after validation error
body: <p>In my form, I generate the select options from my MySQL data table. But when I submit the form without any value, it returns to a blank page and does not show any validation error. When I select a value and submit the form, it inserts the value to MySQL table. No MySQL or PHP error is thrown. I am assuming the problem is the query for the select option is not running. But if it is the problem, what should be the good practice to avoid this problem?
  My form is:</p>
  <pre><code>&lt;?php
     require 'db.php';
     if ($_SERVER[&quot;REQUEST_METHOD&quot;] == &quot;POST&quot;) {
         $ad_di = &quot;&quot;;
         $ad_di_err = &quot;&quot;;
     
         if (empty(trim($_POST[&quot;ad_di&quot;]))) {
             $ad_di_err = &quot;District cannot be empty.&quot;;
         } else {
             $ad_di = trim($_POST[&quot;ad_di&quot;]);
         }
     
         if (empty($ad_di_err)) {
             $sql = &quot;INSERT INTO address (ad_di) VALUES (?)&quot;;
             if ($stmt = $mysqli-&gt;prepare($sql)) {
                 $stmt-&gt;bind_param(&quot;i&quot;, $p_ad_di);
                 $p_ad_di = $ad_di;
                 if ($stmt-&gt;execute()) {
                     $id = $mysqli-&gt;insert_id;
                     header(&quot;location: add.php?id=$id&amp;update=success&quot;);
                 }
                 $stmt-&gt;close();
             } 
         }
     }
     ?&gt;
  &lt;form id=&quot;acEdit&quot; action=&quot;&lt;?php echo htmlspecialchars($_SERVER[&quot;PHP_SELF&quot;]); ?&gt;&quot; method=&quot;post&quot; class=&quot;contact-form&quot;&gt;
     &lt;div class=&quot;row&quot;&gt;
        &lt;div class=&quot;col-3&quot;&gt;
           &lt;div class=&quot;form-group&quot;&gt;
              &lt;label&gt;District&lt;/label&gt;
              &lt;select name='ad_di' id='ad_di' class='custom-select'&gt;
                 &lt;option value=''&gt;Select one ...&lt;/option&gt;
                 &lt;?php
                    $qDistrict = &quot;SELECT * FROM districts order by name ASC&quot;;
                    echo &quot;&lt;select name='ad_di' id='ad_di' class='custom-select'&gt;&lt;option value=''&gt;Select one ...&lt;/option&gt;&quot;;
                    foreach ($mysqli-&gt;query($qDistrict) as $rowDistrict) {
                        echo &quot;&lt;option value={$rowDistrict['id']}&gt;{$rowDistrict['name']}&lt;/option&gt;&quot;;
                    }
                    echo &quot;&lt;/select&gt;&quot;;
                    ?&gt;
              &lt;/select&gt;
              &lt;span class=&quot;invalid-feedback&quot;&gt;&lt;?php echo $ad_di_err; ?&gt;&lt;/span&gt;
           &lt;/div&gt;
        &lt;/div&gt;
     &lt;/div&gt;
  &lt;/form&gt;
  </code></pre>
  <p>The above query for select options returns as following when the page is loaded first,</p>
  <pre><code>&lt;select name=&quot;ad_di&quot; id=&quot;ad_di&quot; class=&quot;custom-select&quot;&gt;
     &lt;option value=&quot;&quot;&gt;Select one ...&lt;/option&gt;
     &lt;option value=&quot;28&quot;&gt;Dist A&lt;/option&gt;
     &lt;option value=&quot;11&quot;&gt;Dist B&lt;/option&gt;
     &lt;option value=&quot;35&quot;&gt;Dist C&lt;/option&gt;
     &lt;option value=&quot;33&quot;&gt;Dist D&lt;/option&gt;
  &lt;/select&gt;
  </code></pre>
  <p>But when the validation error occurs, the returning page doesn't have any options, just returns</p>
  <pre><code>&lt;select name=&quot;ad_di&quot; id=&quot;ad_di&quot; class=&quot;custom-select&quot;&gt;
     &lt;option value=&quot;&quot;&gt;Select one ...&lt;/option&gt;
  &lt;/select&gt;
  </code></pre>