php - Display a set value from a mysqli_query in Select list -


i have form captures username , type (controls user has access to), , writes database. type controlled select box. presently there 3 values possible: 1, 4 , 5.

<?php  $sql1 = "select * `usertype` order `id`"; $result1 = mysqli_query($con, $sql1);  ?> <form action="" method="post">     <ul>         <li>             username:<br>             <input type="text" name="username">         </li>         <li>             user type:<br>             <select name="type">             <?php                 while ($data1 = mysqli_fetch_assoc($result1)) {                     echo '<option value="' . $data1['id'] . '">' . $data1['type'] . '</option>';                 }             ?>             </select>         </li>     </ul> </form> 

i have page on want retrieve single existing user username or type can changed. problem i'm having select box. it's not showing in database. particular user i'm testing with, select box should showing type of 4. instead showing type of 5. code of second page below. doing wrong?

<?php $user_id = 47;  $sql1 = "select * `usertype` order `id`"; $result1 = mysqli_query($con, $sql1);  $sql2 = "select * `users` `users`.user_id = $user_id";  $result2 = mysqli_query($con, $sql2); $user_details = mysqli_fetch_assoc($result2); print_r_html($user_details); ?> <form action="" method="post">     <ul>         <li>             username:<br>             <input type="text" name="username" value="<?php echo $user_details['username'];?>">         </li>          <li>             <select name="type">             <?php                 while ($data1 = mysqli_fetch_assoc($result1))                 {                     echo '<option value="' . $data1['id'] . '" success="' . $user_details['type'] . '">' . $data1['type'] . '</option>';                 }             ?>             </select>         </li>     </ul> </form> 

here array dumb of $user_details:

array (     [user_id] => 47     [username] => paul     [password] => 5f4dcc3b5aa765d61d8327deb882cf99     [first_name] => paul     [last_name] => smith     [email] => paul@somewhere.com     [email_code] => dda1f2a2c5b29f3b28827716c2dffe61     [active] => 1     [password_recover] => 0     [type] => 4     [allow_email] => 1     [profile] => images/profile/6dc76117b3.jpg     [acct_nbr] => xxxxxxxxx (intentionally masked) ) 

the select list have values in it, wrong 1 displaying selected.

p.s. know don't have submit button in form yet.

thanks.

thanks showdev pointing me in right direction. code below accomplished needed. replaces form in second block of code posted in question.

<form action="" method="post">     <ul>         <li>             username:<br>             <input type="text" name="username" value="<?php echo $user_details['username'];?>">         </li>          <li>             <select name="type">             <?php                 while ($data1 = mysqli_fetch_assoc($result1))                 {                     if ($data1['id'] == $user_details['type']) {                         echo '<option value="' . $data1['id'] . '" selected="selected">' . $data1['type'] . '</option>';                     } else {                         echo '<option value="' . $data1['id'] . '">' . $data1['type'] . '</option>';                     }                 }             ?>             </select>         </li>     </ul> </form> 

Comments

Popular posts from this blog

java - How to specify maven bin in eclipse maven plugin? -

single sign on - Logging into Plone site with credentials passed through HTTP -

php - Why does AJAX not process login form? -