douhan5853 2018-07-31 20:18
浏览 51
已采纳

从数据库中检索数据并将其存储在php中的select标记中

I'm working on update user account and my problem is how do i get the value of user type and store it in a combobox or select tag. Here's my code:

<select class="form-control" name="user_type" required>
   <option value=''>Select Type</option>
      <?php
        $query = "SELECT * FROM user_types";
        $result = mysqli_query($connections, $query);
        while($rows = mysqli_fetch_assoc($result)){
          $db_id = $rows["user_type_id"];
          $db_desc = $rows["description"];

          echo "<option value='$db_id'>$db_desc</option>";
       }
      ?>
</select>

From here i can get all of the user type in my database. Here's my user_types table.

enter image description here

And my users table

enter image description here

My question is how do i get the id and value of the user type based on users table and put that in select tag

enter image description here

Here's i can't get the value of user type

  • 写回答

1条回答 默认 最新

  • douji9184 2018-07-31 20:29
    关注

    I'm thinking that you are working on editing users in your system. You would like to have the ability to change their User Type by allowing the logged in person to choose from a select box.

    I suggest the following:

    First you will need to read your User Types table into an array. The array keys need to be the user_type_id and the value needs to be the description.

    When you would show the user_type_id, instead show this new array value for that index.

    When you are displaying this field for editing, do exactly like your code in the original question but also denote the option as "selected" if it matches the user type of the user that you are editing.

    <?php 
    //$user will have all the data for the user you are editing
    $query = "SELECT * FROM user_types";
    $result = mysqli_query($connections, $query);
    $user_types = array();
    while($rows = mysqli_fetch_assoc($result)){
      $db_id = $rows["user_type_id"];
      $db_desc = $rows["description"];
      $user_types[$db_id] = $db_desc;
    }
    
    
    ?>
    <select class="form-control" name="user_type" required>
       <option value=''>Select Type</option>
          <?php
    
            foreach($user_types as $id => $desc){
              if ($user['user_type_id'] == $id){
                echo "<option selected='selected' value='$id'>$desc</option>";
              }else{
                echo "<option value='$id'>$desc</option>";
              }
           }
          ?>
    </select>
    

    When you handle the form being submitted, the value in $_POST['user_type'] will be the user_type_id corresponding to the selected user type.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器