gordsmash Posted September 13, 2009 Share Posted September 13, 2009 Hello guys i need help with making my drop down menu to work with php. Here is the code that i am using... <?php $sql = "SELECT * FROM members"; $query = mysql_query($sql); ?> <select name="members" id="members"> <?php while($row = mysql_fetch_array($query)){ ?> <option> <?php echo $row['user_name']; ?> </option> <?php } ?> </select> Now sometimes the code will actually execute giving me the values i want but most of the time it wont execute leaving the drop down menu blank with nothing in it. If someone could tell me what i need to do with my code that would be awesome. Link to comment https://forums.phpfreaks.com/topic/174090-solved-need-help-with-drop-down-menu/ Share on other sites More sharing options...
Ikram Posted September 13, 2009 Share Posted September 13, 2009 You may use this and edit the values: <?php echo "<form id=\"form1\" name=\"form1\" method=\"post\" action=\"\">\n"; echo " <select name=\"select\">\n"; echo " <option>1</option>\n"; echo " <option>2</option>\n"; echo " <option>3</option>\n"; echo " </select>\n"; echo "</form>\n"; echo "</body>\n"; echo "</html>\n"; ?> Link to comment https://forums.phpfreaks.com/topic/174090-solved-need-help-with-drop-down-menu/#findComment-917690 Share on other sites More sharing options...
cbolson Posted September 13, 2009 Share Posted September 13, 2009 Now sometimes the code will actually execute giving me the values i want but most of the time it wont execute leaving the drop down menu blank with nothing in it. If someone could tell me what i need to do with my code that would be awesome. If it works sometimes, the problem is not with that block of code (not that I would do it like that), it must be with something else. You say that it leaves the drop down menu blank - do you mean that it has various empty values or that it has no elements? Just for reference an incase you are interested, I would do this bit of code something like this: // get list of members from the database $sql = "SELECT user_name FROM members"; $res = mysql_query($sql) or die("Error getting members"); $list_members=""; while($row = mysql_fetch_assoc($res)){ $list_members.='<option value="'.$row["user_name"].'">'.$row["user_name"].'</option>'; } ?> <select name="members" id="members"> <?php echo $list_members; ?> </select> Link to comment https://forums.phpfreaks.com/topic/174090-solved-need-help-with-drop-down-menu/#findComment-917707 Share on other sites More sharing options...
gordsmash Posted September 13, 2009 Author Share Posted September 13, 2009 Ok i found out what the problem was. Thank to the code cbolson suggested... $res = mysql_query($sql) or die("Error getting members"); It gave me the "Error getting members". So i found out it wasnt connecting to the table for some reason and i fixed it. I had to put... $sql = "SELECT user_name FROM gs_uploads.members"; to fix it. Thanks a lot cbolson for helping me out buddy!! Link to comment https://forums.phpfreaks.com/topic/174090-solved-need-help-with-drop-down-menu/#findComment-917713 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.