lucy Posted August 27, 2009 Share Posted August 27, 2009 How can i populate a combo box from a specific field from a mysql database? I would like the combo box to display all different entries in a particular field of a table. Thanks, Lucy Quote Link to comment https://forums.phpfreaks.com/topic/172113-solved-populate-combo-box-from-mysql-database/ Share on other sites More sharing options...
lucy Posted August 27, 2009 Author Share Posted August 27, 2009 Ive done it. It was a google job. <?php $user = BLANK; $password = BLANK; $database = BLANK; // Connect to the database $con = mysql_connect("localhost",$user,$password) or die ('Could not connect: ' . mysql_error()); mysql_select_db($database, $con); // Create the form, post to the same file echo "<form method='post' action='combo.php'>"; // Form a query to populate the combo-box $query = "SELECT DISTINCT equipment FROM table1;"; // Successful query? if($result = mysql_query($query)) { // If there are results returned, prepare combo-box if($success = mysql_num_rows($result) > 0) { // Start combo-box echo "<select name='item'>\n"; echo "<option>-- Equipment --</option>\n"; // For each item in the results... while ($row = mysql_fetch_array($result)) // Add a new option to the combo-box echo "<option value='$row[equipment]'>$row[equipment]</option>\n"; // End the combo-box echo "</select>\n"; } // No results found in the database else { echo "No results found."; } } // Error in the database else { echo "Failed to connect to database."; } // Add a submit button to the form echo "<input type='submit' value='Submit' /></form>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/172113-solved-populate-combo-box-from-mysql-database/#findComment-907503 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.