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:
As noted above I need the list in the picture to just show Administrator and User.