Willpool Posted January 13, 2014 Share Posted January 13, 2014 Hi. Sorry if I've posted in the wrong forum or something - I joined this forum a few seconds ago. I'm trying to create something which displays all of the users in a database table in a drop-down menu. I've tried this code: <?php include 'config.php'; session_start(); $suser = $_SESSION['user']; $con = mysql_connect($config['mysql_host'], $config['mysql_user'], $config['mysql_pass']); mysql_select_db($config['database']); $cpuTable = $config['cpuTable']; $query = mysql_query("SELECT * FROM $cpuTable WHERE suser='$suser'"); while($row = mysql_fetch_assoc($qa)) { $users = $row['username']; echo " <select name='cpuser'> <option value='user'>$users</option> </select>"; } I created two users in the database - "cp_test1" and "cp_test2", tried the code and it outputted this: Instead of listing the users in one drop-down menu, it creates two and lists one in each. Can anyone help? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/285336-list-all-users-in-a-database-in-a-drop-down-menu/ Share on other sites More sharing options...
Will_ Posted January 13, 2014 Share Posted January 13, 2014 Ignore the accidental $qa instead of $query. That's not in the real code. (I lost access to my other account so I can't edit it) Quote Link to comment https://forums.phpfreaks.com/topic/285336-list-all-users-in-a-database-in-a-drop-down-menu/#findComment-1465092 Share on other sites More sharing options...
ginerjm Posted January 13, 2014 Share Posted January 13, 2014 YOu are outputting a select tag for every user. Move that outside of the while loop Quote Link to comment https://forums.phpfreaks.com/topic/285336-list-all-users-in-a-database-in-a-drop-down-menu/#findComment-1465095 Share on other sites More sharing options...
Barand Posted January 13, 2014 Share Posted January 13, 2014 your option value should also be '$users' otherwise they will all have the same value when selected Quote Link to comment https://forums.phpfreaks.com/topic/285336-list-all-users-in-a-database-in-a-drop-down-menu/#findComment-1465102 Share on other sites More sharing options...
Will_ Posted January 13, 2014 Share Posted January 13, 2014 Changed it to: while($row = mysql_fetch_assoc($query)) { $users = $row['username']; } echo " <select name='cpuser'> <option value='$users'>$users</option> </select> "; It now outputs one drop-down menu but it only displays one of the users. Quote Link to comment https://forums.phpfreaks.com/topic/285336-list-all-users-in-a-database-in-a-drop-down-menu/#findComment-1465108 Share on other sites More sharing options...
Solution Barand Posted January 13, 2014 Solution Share Posted January 13, 2014 Not surprising, you moved it all outside the loop echo "<select name='cpuser'>"; while($row = mysql_fetch_assoc($query)) { $users = $row['username']; echo "<option value='$users'>$users</option>" } echo "</select>"; Quote Link to comment https://forums.phpfreaks.com/topic/285336-list-all-users-in-a-database-in-a-drop-down-menu/#findComment-1465120 Share on other sites More sharing options...
Will_ Posted January 14, 2014 Share Posted January 14, 2014 Not surprising, you moved it all outside the loop echo "<select name='cpuser'>"; while($row = mysql_fetch_assoc($query)) { $users = $row['username']; echo "<option value='$users'>$users</option>" } echo "</select>"; Thanks, it works now. Quote Link to comment https://forums.phpfreaks.com/topic/285336-list-all-users-in-a-database-in-a-drop-down-menu/#findComment-1465193 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.