SitePoint Sponsor

User Tag List

Results 1 to 19 of 19
  1. #1
    SitePoint Addict Johana Solendhal's Avatar
    Join Date
    Aug 2004
    Location
    Land of the free
    Posts
    252
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Populating a dropdown with database data

    Hello guys!

    I am need to fetch data from a database and populate a dropdown select with a specific table's values.

    After the values are displayed, the user would be able to select an option, and that selection would "trigger" simple math operations.

    But first I need to sucessfully populate the dropdown, which I am failing miserably right now.

    Here's what I have done so far:

    PHP Code:
    CREATE TABLE `members` (
    `
    idint(10NOT NULL auto_increment,
    `
    usernametext NOT NULL,
    `
    passwordtext NOT NULL,
    `
    emailtext NOT NULL,
    `
    packtext NOT NULL,
    `
    packdescriptiontext NOT NULL,
    PRIMARY KEY (id)
    ); 
    PHP Code:
    <?php mysql_connect($config['sql_host'], $config['sql_username'], $config['sql_password']);
        
    mysql_select_db($config['sql_database']);
            
    $sql mysql_query("SELECT members FROM pack WHERE pack = $pack");   

    echo 
    "<tr><td>Please choose: ";
    echo 
    "<select name='pack' size='1'>";
    while (
    $r mysql_fetch_array($sql))
    {
        echo 
    "<option value='"$r['pack'] ."'>"$r['pack'] ."</option>";
    }
    echo 
    "</select></td></tr>"?>

  2. #2
    Formerly known as RockNRollPig Shpigford's Avatar
    Join Date
    Dec 2002
    Location
    Denver, CO
    Posts
    2,877
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't know why that wouldn't work. What specifically doesn't work about? Do you get a certain error? Give as many details as you can.

  3. #3
    SitePoint Evangelist Aska's Avatar
    Join Date
    Aug 2003
    Location
    Melbourne, Australia
    Posts
    454
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $sql mysql_query("SELECT members FROM pack WHERE pack = $pack"); 
    Is `members` the only table in the database? If so, this query is weird.

  4. #4
    Formerly known as RockNRollPig Shpigford's Avatar
    Join Date
    Dec 2002
    Location
    Denver, CO
    Posts
    2,877
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Doh. Aska brought my attention to it and probably answered your question. Your query isn't selecting anything. You've only selected "members" from the table "pack" so there's no way that "pack" could equal anything because you didn't select "pack" from the table. Maybe you have your tables and fields mixed up or something.

  5. #5
    SitePoint Addict Johana Solendhal's Avatar
    Join Date
    Aug 2004
    Location
    Land of the free
    Posts
    252
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you for the response loves.

    Yeah, it's the only database I have. I don't get any particular error, the dropdown doesn't echo the data.

  6. #6
    SitePoint Addict Johana Solendhal's Avatar
    Join Date
    Aug 2004
    Location
    Land of the free
    Posts
    252
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by RockNRollPig
    Doh. Aska brought my attention to it and probably answered your question. Your query isn't selecting anything. You've only selected "members" from the table "pack" so there's no way that "pack" could equal anything because you didn't select "pack" from the table. Maybe you have your tables and fields mixed up or something.
    so should I make another query selecting "pack"?

    Be gentle with me guys, I'm a simple Designer.

  7. #7
    Formerly known as RockNRollPig Shpigford's Avatar
    Join Date
    Dec 2002
    Location
    Denver, CO
    Posts
    2,877
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Johana Solendhal
    so should I make another query selecting "pack"?

    Be gentle with me guys, I'm a simple Designer.
    Hey, not a problem at all. You gotta start learning somewhere. At any rate, you don't need another query. You need to fix the one you've got. If you could list the name of your table and then the fields within that table we'll be able to help you better.

  8. #8
    He's No Good To Me Dead silver trophybronze trophy stymiee's Avatar
    Join Date
    Feb 2003
    Location
    Slave I
    Posts
    23,424
    Mentioned
    2 Post(s)
    Tagged
    1 Thread(s)
    $sql = mysql_query("SELECT members FROM pack WHERE pack = $pack");

    You have the field name and table name backwards for starters. And, even if you reversed them, the value for $r['pack'] is going to be the same for every row anyway.

  9. #9
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,235
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by RockNRollPig
    Hey, not a problem at all. You gotta start learning somewhere. At any rate, you don't need another query. You need to fix the one you've got. If you could list the name of your table and then the fields within that table we'll be able to help you better.
    Off Topic:

    She's got the SQL to create that table in her first post.

  10. #10
    Formerly known as RockNRollPig Shpigford's Avatar
    Join Date
    Dec 2002
    Location
    Denver, CO
    Posts
    2,877
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by vgarcia
    Off Topic:

    She's got the SQL to create that table in her first post.
    Snap. I'm an idiot.

  11. #11
    He's No Good To Me Dead silver trophybronze trophy stymiee's Avatar
    Join Date
    Feb 2003
    Location
    Slave I
    Posts
    23,424
    Mentioned
    2 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by RockNRollPig
    Snap. I'm an idiot.
    Off Topic:

    No you're not. You just suffer from "Stymiee Disease". You miss the totally obvious.

  12. #12
    SitePoint Addict Johana Solendhal's Avatar
    Join Date
    Aug 2004
    Location
    Land of the free
    Posts
    252
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Gracias everyone!

    So am I doing anythign wrong besides what stymiee pointed out?

  13. #13
    Formerly known as RockNRollPig Shpigford's Avatar
    Join Date
    Dec 2002
    Location
    Denver, CO
    Posts
    2,877
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Johana Solendhal
    Gracias everyone!

    So am I doing anythign wrong besides what stymiee pointed out?
    Not that I can tell. Did it work?

  14. #14
    SitePoint Addict Johana Solendhal's Avatar
    Join Date
    Aug 2004
    Location
    Land of the free
    Posts
    252
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sure did! You guys are angels!

    You guys are probably going to kill me or redirect me to a good tutorial for this but what if I wanted to be able to edit the numeric data within the “pack” column? The user would choose the value he would like to edit from the drop down menu, that value would be “echoed” in an input box, where the user would edit it and press the update button, and voila!

    Do you loves have any tip on how to achieve this easly?

  15. #15
    SitePoint Wizard Pedro Monteiro's Avatar
    Join Date
    Sep 2002
    Location
    Lisbon
    Posts
    1,393
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Johana, Just a little suspition that I have, could you show us how you are connection to the database? Give us an example of your config.php file.

  16. #16
    SitePoint Addict Johana Solendhal's Avatar
    Join Date
    Aug 2004
    Location
    Land of the free
    Posts
    252
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Mmm alrighty.

    Code:
    My Config.php
    <?php
    
    $config = array(
    
    
    //Mysql Settings
    	"sql_host" => "localhost",
    	"sql_database" => "mydatabase",
    	"sql_username" => "myusername",
    	"sql_password" => "mypassword",
    
    );
    ?>
    My Index.php
    Code:
    <?phpinclude "config.php";?> 
    
    <?php mysql_connect($config['sql_host'], $config['sql_username'], $config['sql_password']); 
        mysql_select_db($config['sql_database']); 
            $sql = mysql_query("SELECT members FROM pack WHERE pack = $pack");    
    
    echo "<tr><td>Please choose: "; 
    echo "<select name='pack' size='1'>"; 
    while ($r = mysql_fetch_array($sql)) 
    { 
        echo "<option value='". $r['pack'] ."'>". $r['pack'] ."</option>"; 
    } 
    echo "</select></td></tr>"; ?>

  17. #17
    SitePoint Addict Johana Solendhal's Avatar
    Join Date
    Aug 2004
    Location
    Land of the free
    Posts
    252
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Of course that I rectified the code with stymiee's correction to:
    $sql = mysql_query("SELECT packs FROM members ");

  18. #18
    SitePoint Wizard Pedro Monteiro's Avatar
    Join Date
    Sep 2002
    Location
    Lisbon
    Posts
    1,393
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Forget about me, everything is oki. About the editing of the data in an input box, I would love to help you out but my PHP knowledge is very limited, but I do know that what you are trying to do doesn't cover that much ground. Stymiee any of the other "heavy brains" here might be able to help you out easly on the subject.

    Good luck!

  19. #19
    SitePoint Addict Johana Solendhal's Avatar
    Join Date
    Aug 2004
    Location
    Land of the free
    Posts
    252
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Gracias Pedro.


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
  •