如何在多个 select 中保留 selected 值,例如 boostrap-select

How to retain the selected value in multiple select like boostrap-select

我想在提交表单后保留我选择的值。

<select name="student[]" class="selectpicker" multiple title="Click here.." data-width="auto" data-live-search="true" required>
  <?php  
    if (isset($_POST['submit'])){
    $selected = $_POST['studname'];
    $qry = "Select studtbl.stud_id,concat(studtbl.fname,' ', 
    substring(studtbl.mname, 1,1),'. ',studtbl.lname) as Name from studtbl";
  $result = mysqli_query($conn, $qry);
  while($row = mysqli_fetch_array($result))
  {
  extract($row);
  echo '<option value="'.$stud_id.'" '.(($stud_id == 
        $selected)? 'selected="selected"':"" >'.$Name.'</option>';
  } 
 }else{
    $qry = "Select studtbl.stud_id,concat(studtbl.fname,' ', 
    substring(studtbl.mname, 1,1),'. ',studtbl.lname) as Name from studtbl";
    $result = mysqli_query($conn, $qry);
    while($row = mysqli_fetch_array($result))
    {
     extract($row);
     echo '<option value="'.$stud_id.'" >'.$Name.'</option>';
    } 
 }
    ?>
</select>
<input type='submit' class="btn btn-primary" name='submit' value='Create 
     Account' />

我的问题是提交后没有保留

<select name="student[]" class="selectpicker" multiple title="Click here.." data-width="auto" data-live-search="true" required>
  <?php     
    if (isset($_POST['submit'])){
    $selected = $_POST['student']; // <---- multi-selects come in as Array
    $qry = "Select studtbl.stud_id,concat(studtbl.fname,' ', 
    substring(studtbl.mname, 1,1),'. ',studtbl.lname) as Name from studtbl";
        $result = mysqli_query($conn, $qry);
        while($row = mysqli_fetch_array($result))
        {
        extract($row);
        // if the current ID is inside the POST-ed Array - mark as SELECTED
        echo '<option value="'.$stud_id.'" '.(in_array($stud_id, $selected) ? 'selected="selected"': "").'>'.$Name.'</option>';
        } 
    }else{
    $qry = "Select studtbl.stud_id,concat(studtbl.fname,' ', 
    substring(studtbl.mname, 1,1),'. ',studtbl.lname) as Name from studtbl";
                $result = mysqli_query($conn, $qry);
                while($row = mysqli_fetch_array($result))
                {
                    extract($row);
                    echo '<option value="'.$stud_id.'" >'.$Name.'</option>';
                } 
    }
    ?>
</select>
<input type='submit' class="btn btn-primary" name='submit' value='Create 
     Account' />