Jamesking56 Posted July 26, 2008 Share Posted July 26, 2008 How can I populate a form Drop Down Box using PHP and a MySQL Database??I have tried before: <form><select name="select"><?php$sql = "SELECT * FROM country";$query = mysql_query($sql); $data = mysql_fetch_array($query);foreach( $data as $row ){ echo "<option value=" . $row['value'] . ">" . $row['country'] . "</option>";} ?></select></form> but for some reason it doesn;t display the values right, anyone help? Link to comment Share on other sites More sharing options...
onno.venker Posted July 26, 2008 Share Posted July 26, 2008 You forgot the qoutes. while($data = mysql_fetch_array($query)){ echo "<option value=\"" . $data ['id'] . "\">" . $data ['country'] . "</option>";} btw, why with array. you're selecting the whole table. use the id for the values. array's start at 0 instead of 1. Link to comment Share on other sites More sharing options...
justsomeguy Posted July 26, 2008 Share Posted July 26, 2008 If you want to refer to your fields by name instead of index then use mysql_fetch_assoc instead of mysql_fetch_array.$data = mysql_fetch_assoc($query); Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.