Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > PHP/MySQL > PHP Databases
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
PHP Databases Using PHP in conjunction with databases. PHP questions not specific to databases should be directed to one of the other PHP forums.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the PHP Databases section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old June 22nd, 2006, 10:17 PM
Registered User
 
Join Date: Oct 2005
Location: Steger, Illinois, USA.
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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
Reply With Quote
  #2 (permalink)  
Old June 23rd, 2006, 10:46 PM
richard.york's Avatar
Wrox Author
Points: 5,506, Level: 31
Points: 5,506, Level: 31 Points: 5,506, Level: 31 Points: 5,506, Level: 31
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Camby, IN, USA.
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

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
Reply With Quote
  #3 (permalink)  
Old June 24th, 2006, 01:38 PM
Registered User
 
Join Date: Oct 2005
Location: Steger, Illinois, USA.
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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
Reply With Quote
  #4 (permalink)  
Old June 24th, 2006, 09:23 PM
richard.york's Avatar
Wrox Author
Points: 5,506, Level: 31
Points: 5,506, Level: 31 Points: 5,506, Level: 31 Points: 5,506, Level: 31
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Camby, IN, USA.
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

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
Reply With Quote
  #5 (permalink)  
Old June 24th, 2006, 09:38 PM
Registered User
 
Join Date: Oct 2005
Location: Steger, Illinois, USA.
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Populate ListView From ComboBox Select eusanpe C# 4 August 20th, 2007 06:49 PM
dependent select menu hastikeyvan Classic ASP Professional 3 November 10th, 2006 01:46 PM
populate Menus from Database harrysingh26 .NET Framework 2.0 3 March 2nd, 2006 07:54 AM
dependent select menu hastikeyvan Dreamweaver (all versions) 1 February 27th, 2006 04:34 AM
Store Items from dropdown menu in mysql database scoobie PHP Databases 0 April 28th, 2005 08:17 AM



All times are GMT -4. The time now is 12:53 AM.


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