p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   PHP Databases (http://p2p.wrox.com/forumdisplay.php?f=97)
-   -   Populate Select Menu from MySQL Database (http://p2p.wrox.com/showthread.php?t=44381)

joviyach June 22nd, 2006 10:17 PM

Populate Select Menu from MySQL Database
 
I am starting to delve into more advanced PHP/MySQL development on my own and I have been trying to Google a good example of this for three nights now, and have found nothing very useful. I am hoping I can find some help here...

I have a MySQL database with 8 fields (uniqid, firstname, lastname, street, city, state, zipcode, phone) and I want the select menu to populate with the uniqid, firstname, lastname, and maybe one other field. I can't find any good examples of this that don't rely on Javascript or something like that. Surely there is a way to do this with PHP right?

Any help would greatly be appreciated.

Thanks,

Jim

Jim

richard.york June 23rd, 2006 10:46 PM

Maybe you're having trouble finding tutorials because you don't know the right terms to query!

Assuming you know how to connect to the database, you'd populate a <select> element like so,
Code:

$query = mysql_query(
    "SELECT `uniqid`,
            `firstname`,
            `lastname`
       FROM `tablename`
   ORDER BY `lastname` ASC"
);

echo "<select name='people'>\n";

while ($data = mysql_fetch_array($query, MYSQL_ASSOC))
{
    echo "    <option value='{$data['uniqid']}'>{$data['lastname']}, {$data['firstname']}</option>\n";
}

echo "</select>\n";

See, pretty easy stuff. Let me know if you have any questions.

HTH!

Regards,
Rich

--
Author,
Beginning CSS: Cascading Style Sheets For Web Design
CSS Instant Results

http://www.catb.org/~esr/faqs/smart-questions.html

joviyach June 24th, 2006 01:38 PM

First of all, thanks a lot! It works great except for one thing, it does not populate the uniqid field still. Does it not work for auto_incrementing INT values or something?

Thanks,

Jim

Jim

richard.york June 24th, 2006 09:23 PM

What does this query produce when you run it at the mysql command line or from something like PHPMyAdmin?
Code:

     SELECT `uniqid`,
            `firstname`,
            `lastname`
       FROM `tablename`
   ORDER BY `lastname` ASC

Are you seeing the id? Are you seeing any errors? Does the HTML look alright when you view the source?

You could also add the following after the query, building in error reporting is always a good idea.
Code:

if (!$query)
{
    echo mysql_error()."<br />\n";
}

In answer to your question, of course you can use an auto incrementing integer field! ;) I remember asking questions like these when I was a beginner; Rule of thumb, if it seems obvious and like it would save time, assume the language/technology supports it!

Regards,
Rich

--
Author,
Beginning CSS: Cascading Style Sheets For Web Design
CSS Instant Results

http://www.catb.org/~esr/faqs/smart-questions.html

joviyach June 24th, 2006 09:38 PM

Thanks for the reply.

I did add a line in there after the query for error reporting. It does not produce an error, it's like it just ignores the part that asks for the "uniqid" field. The output is the drop down menu with the lastname, firstname in it. Very strange, and I can't figure out why. I even opened up phpMyAdmin to make sure I was using the correct field name there, and did not make a typo in the name, and I did not. It's just ignoring for some reason.

Jim


All times are GMT -4. The time now is 07:18 AM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.