Jump to content

[SOLVED] populate combo box from mysql database


lucy

Recommended Posts

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>";
  
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.