SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast
    Join Date
    Nov 2010
    Location
    UK
    Posts
    26
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP & Mysql help needed to populate drop down list

    Can anyone tell me how I'd go about populating a drop down list from a mysql database using php?

    I have a database called "test". In it I have two tables, one called "manufacturers"

    It contains "manufacturer_id", and "manufacturer_name"

    The other table is called "models"

    This contains "manufacturer_id", "model_id", and "model_name"

    My goal is to have a dropdown list where a person selects a manufacture, and then a second drop down where they can select a model made by that manufacturer.

    Finally when using selecting the model from the second drop down, they should be directed to a model specific web page.

    I can connect to the database (i think) and close the database, but none of guides i've tried to follow for the actual function work for me, with my limited knowledge.

    Here is where i'm up to with my code so far

    config.php
    Code:
    <?php
    $dbhost = 'localhost';
    $dbuser = 'username';
    $dbpass = 'password';
    ?>

    openfinder.php
    Code:
    <?php
    // This is an example opendb.php
    $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die                      ('Error connecting to mysql');
    mysql_select_db($dbname);
    ?>
    partfinder.php
    Code:
    <?php
    include 'config.php';
    include 'openfinder.php';
    
    
    echo "<select>";
    
    $result = mysql_query("SELECT * FROM manufacturer");
    
    while($row = mysql_fetch_array($result))
      {
      echo "<option value=". $row["manufacturer_id"] .">". $row["manufacturer_name"] . "</option>";
      }
    echo "</select>";
    
    
    include 'closefinder.php';
    
    ?>
    closefinder.php
    Code:
    <?php
    // an example of closedb.php
    // it does nothing but closing
    // a mysql database connection
    
    mysql_close($conn);
    ?>
    Basically i'm trying to create a part finer similar to the way it works on the page below:

    http://www.ngkpartfinder.co.uk/motor...PARK%2520PLUGS

    Can anyone of you guys help?

  2. #2
    SitePoint Member
    Join Date
    Jun 2012
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you must add in your config.php the following line:

    $dbname='test';

    and if your table is manufacturers you need to edit a $result = mysql_query("SELECT * FROM manufacturers");

  3. #3
    SitePoint Enthusiast
    Join Date
    Nov 2010
    Location
    UK
    Posts
    26
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Excellent.

    it was just dbname='test'; that i needed.

    my table was in deed manufacturer.

    Now I just need to figure out how on selection of the manufacturer, to get another drop down list to appear populated by that manufacturer's models.

  4. #4
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Imagine this is the output of the html generated by the page you already described:
    Code:
    <form action = "part_by_manufacturer.php" method = POST>
    <select name="manufacturer_id">
    <option value=1 >Ford</option>
    <option value=2 >GM</option>
    </select>
    <input type=submit>
    </form>
    Then this is a simple example of what the post back form handler would then do:

    part_by_manufacturer.php
    PHP Code:
    <?php
    if( isset($_POST['manufacturer_id'])){

    // turn the incoming var into an integer in case someone
    // is trying an sql injection attack on you
    $id = (int)$_POST['manufacturer_id'];

    // inc db connection etc

    // build your sql query
    $qry "select model from models where manufacturer_id = $id";

    // a line of debug to make sure it worked as planned
    echo $sql;

    // now display the results

    }else{

    echo 
    "go back and pick a manufacturer please";
    }


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •