whelpton Posted March 14, 2009 Share Posted March 14, 2009 Hi guys, I was wondering weather it would be possible to take the contents of a mysql table and insert it into a drop down form. My mysql table is called "Users" and I want to take the row "Username" and output it so that it displays all the current usernames in a form field such as: <select name="select" id="select"> <option>USERNAME1</option> <option>USERNAME2</option> </select> Obviously replacing the static username 1 and username 2 with the content from the mysql table Link to comment https://forums.phpfreaks.com/topic/149351-solved-placing-mysql-into-drop-down-form/ Share on other sites More sharing options...
ccchan Posted March 14, 2009 Share Posted March 14, 2009 Would it look something like this: <select class="input2" name="username"> </option><? $query = "SELECT * FROM user ORDER BY username ASC"; $result = @mysql_query($query); if ($result){ while ($row = mysql_fetch_array($result, MYSQL_ASSOC)){ echo '<option value="'.$row['username'].'"'; if ($_POST['username'] = $row['username']){echo 'selected="selected"';} echo '>'.$row['username'].'</option>';}} ?></select> I actually totally made this for something that I made a while back. Adjust it accordingly... If it works for you at all... I'm kind of a noob. Link to comment https://forums.phpfreaks.com/topic/149351-solved-placing-mysql-into-drop-down-form/#findComment-784404 Share on other sites More sharing options...
Festy Posted March 14, 2009 Share Posted March 14, 2009 Would it look something like this: <select class="input2" name="username"> </option><? $query = "SELECT * FROM user ORDER BY username ASC"; $result = @mysql_query($query); if ($result){ while ($row = mysql_fetch_array($result, MYSQL_ASSOC)){ echo '<option value="'.$row['username'].'"'; if ($_POST['username'] = $row['username']){echo 'selected="selected"';} echo '>'.$row['username'].'</option>';}} ?></select> I actually totally made this for something that I made a while back. Adjust it accordingly... If it works for you at all... I'm kind of a noob. That should work ok. Just use '<?php' instead of '<?'. Because you could get errors if short tags are not on in php.ini Link to comment https://forums.phpfreaks.com/topic/149351-solved-placing-mysql-into-drop-down-form/#findComment-784408 Share on other sites More sharing options...
whelpton Posted March 14, 2009 Author Share Posted March 14, 2009 That worked perfectly, thank you so much Link to comment https://forums.phpfreaks.com/topic/149351-solved-placing-mysql-into-drop-down-form/#findComment-784634 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.