Data from a mysql table to populate a pull down menu selection.

Discussion in 'PHP' started by Matt Ridge, Jan 23, 2012.

  1. #1
    Ok, this is what I have.

    
    <?php
    require_once('tb/connectvars.php');
        $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
    
    
            echo '<div id="box4">';
    echo '<h2>Fabricators</h2>';
                echo '<div id="fab1">';
    
    
                echo '<h2>Fabricators</h2>';
    $mysqli = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME); 
    $mysqli->select_db('user');
    $result = $mysqli->query("SELECT * FROM user"); 
    echo "<select name='fab1'>\n";
    while($row = $result->fetch_assoc())
    {
        echo '<option value="' . $row['user'] . '"';
        if($row['user'] == $user)
        {
             echo ' selected';
        }
        echo '>' . $row['user'] . '</option>\n';
    }
    ?>
    
    
    <body>
    <a href="http://kaboomlabs.com/testbed/box.zip">Right Click and Save link as... for Code</a>
    
    
    </body>
    
    Code (markup):
    What it needs to do is to show the name Andy Kahl which is already inside the database but it's not. Can someone look at this code and tell me why?

    Thanks in advance.
     
    Last edited: Jan 23, 2012
    Matt Ridge, Jan 23, 2012 IP
  2. dujmovicv

    dujmovicv Member

    Messages:
    62
    Likes Received:
    2
    Best Answers:
    4
    Trophy Points:
    48
    #2
    1. why don't place the h2, select, etc. part also in the <body> section??? (BTW that's not the problem with your script, just a notice)
    2. the line where you select your DB :
    
    $mysqli->select_db('user');
    
    Code (markup):
    and then your query :
    
    $result = $mysqli->query("SELECT * FROM user");
    
    Code (markup):
    unless you use the SAME name for your database AND your TABLE (user), this could be the problem...
    3. you haven't closed the select tag

    If I were you I'd try this :
    
    <body>
    <?php
    require_once('tb/connectvars.php');
    $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
    
    echo '<div id="box4">';
    echo '<h2>Fabricators</h2>';
    echo '<div id="fab1">';
    
    
    echo '<h2>Fabricators</h2>';
    $mysqli = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME); 
    $mysqli->select_db('database_name');
    $result = $mysqli->query("SELECT * FROM table_name"); 
    echo "<select name='fab1'>";
    while($row = $result->fetch_assoc())
    {
        echo '<option value="' . $row['user_name'] . '"';
        if($row['user_name'] == $user)
        {
             echo ' selected';
        }
        echo '>' . $row['user_name'] . '</option>';
    }
    echo "</select>";
    ?>
    
    <a href="http://kaboomlabs.com/testbed/box.zip">Right Click and Save link as... for Code</a>
    
    </body>
    
    PHP:
    Good luck! :)
     
    dujmovicv, Jan 24, 2012 IP
  3. Matt Ridge

    Matt Ridge Peon

    Messages:
    166
    Likes Received:
    0
    Best Answers:
    1
    Trophy Points:
    0
    #3
    As for your question, the reason I didn't format this at all is because it is part of a much, much larger script. There is a bunch of formatting already involved with it, and right now this is just to show this script, and what it does, and what I want it to do, instead of making it look pretty :)

    I just tried the script, and I now get a blank pull down menu... never mind, I realized what you did, I'll get back on an answer in a second.
     
    Matt Ridge, Jan 24, 2012 IP
  4. Matt Ridge

    Matt Ridge Peon

    Messages:
    166
    Likes Received:
    0
    Best Answers:
    1
    Trophy Points:
    0
    #4
    Ok, I tried your script, I am getting a little lost. Here is the issue.

    I have access to two tables in one database. The database name for this instance is DB1, the tables involved are user, and ncmr.

    Here are the fields and tables in question that are being used.

    In user, the field $user is being accessed, it has a list of all employees to create a pull down menu.

    in ncmr $fab1 is where the data from the table user is being posted to as record.

    What I need to do is this:

    If an name has been entered into $fab1 I need to have a pull down menu show said name as it's selected choice, but the pull down list is from user, not ncmr, so this is why I have links to two tables. And this is why I am getting stuck.

    Does this make any sense?
     
    Last edited: Jan 24, 2012
    Matt Ridge, Jan 24, 2012 IP
  5. Matt Ridge

    Matt Ridge Peon

    Messages:
    166
    Likes Received:
    0
    Best Answers:
    1
    Trophy Points:
    0
    #5
    If people need the script, and the two tables, I have attached here the information.
     

    Attached Files:

    • box.zip
      File size:
      2.7 KB
      Views:
      39
    Matt Ridge, Jan 25, 2012 IP