dpxyfa4718 2014-12-28 00:35
浏览 20
已采纳

从mysql回忆下拉菜单需要独特的选择

I am learning PHP and MySQL and in doing so I am trying to do a little database driven website. I am tinkering with database driven dropdown form lists and so far can display them on the data entry form, can capture the result and insert it into database and now I am trying to recall the selected item, display it on the update/edit form. So far with the below code I can recall the data into update/edit form on a dropdown list however I want to narrow the selectable items down to just the unique roles.

My code is:

<?php

include $_SERVER['DOCUMENT_ROOT']."/connections/connectdb.php";

$role_query = "SELECT fer_users.id fer_roles_id, fer_roles.description
"
    . "FROM fer_users
"
    . "JOIN fer_roles
"
    . "WHERE fer_users.fer_roles_id=fer_roles.id";
$role_result = mysqli_query($connection, $role_query);
?>

<select name="update_role">
    <?php 
    while ($role_row = mysqli_fetch_assoc($role_result)) {  
    $role_id=$role_row["fer_roles_id"];
    $role_description=$role_row["description"];
    $selected = ($role_row['fer_roles_id'] == $id);
    echo "<option value=\"$role_id\" ".($selected ? " selected=\"selected\"":"").">$role_description</option>";
    }
    ?>
</select>  

The result of the SQL query is as follows:

id |     role
-----------------
8  | Administrator
16 | Administrator
9  |     User
14 |     User
15 |     User

And my drop down list renders as follows: enter image description here

As noted above I need the list in the picture to just show Administrator and User.

  • 写回答

4条回答 默认 最新

  • duanpu8830 2014-12-28 03:31
    关注

    Just run two queries.

    <?php
    
    include $_SERVER['DOCUMENT_ROOT']."/connections/connectdb.php";
    
    $role_query = "SELECT fer_roles.id, fer_roles.description
    "
    . "FROM fer_roles
    "
    . "WHERE 1";
    $role_result = mysqli_query($connection, $role_query);
    $role_query2 = "SELECT fer_users.id, fer_users.fer_roles_id 
    "
    . "FROM fer_users
    "
    . "WHERE id = '".mysql_real_escape_string($_GET['id'])."'"; //this assumes you're passing the user as a GET variable
    $role_result2 = mysqli_query($connection, $role_query2);
    $role_row2 = mysqli_fetch_assoc($role_result2);
    ?>
    
    <select name="update_role">
    <?php 
    while ($role_row = mysqli_fetch_assoc($role_result)) {  ?>
        <option value="<?php echo $role_row['id'];?>" <?php if($role_row['id'] == $role_row2['fer_roles_id']){ echo 'selected="selected"';}?>><?php echo $role_row['description'];?></option>";
    }
    ?>
    </select>  
    

    May not be the most elegant answer but it should get the job done :)

    Edit: the above is if you're trying to update one specific user. If you want a list of all users and their roles this would do it:

    <?php
    
    include $_SERVER['DOCUMENT_ROOT']."/connections/connectdb.php";
    
    $role_query = "SELECT fer_roles.id, fer_roles.description
    "
    . "FROM fer_roles
    "
    . "WHERE 1";
    $role_result = mysqli_query($connection, $role_query);
    $role_query2 = "SELECT fer_users.id, fer_users.fer_roles_id 
    "
    . "FROM fer_users
    "
    . "WHERE 1"; //this assumes you're looking up a specific user
    $role_result2 = mysqli_query($connection, $role_query2);
    while($role_row2 = mysqli_fetch_assoc($role_result2)){
    ?>
    
        <select name="update_role">
        <?php 
        while ($role_row = mysqli_fetch_assoc($role_result)) {  ?>
            <option value="<?php echo $role_row['id'];?>" <?php if($role_row['id'] == $role_row2['fer_roles_id']){ echo 'selected="selected"';}?>><?php echo $role_row['description'];?></option>";
        }
    ?>
    </select>  
    <?php
    } ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题