 |
| 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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

June 22nd, 2006, 10:17 PM
|
|
Registered User
|
|
Join Date: Oct 2005
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|

June 23rd, 2006, 10:46 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
|
|
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
|
|

June 24th, 2006, 01:38 PM
|
|
Registered User
|
|
Join Date: Oct 2005
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|

June 24th, 2006, 09:23 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
|
|
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
|
|

June 24th, 2006, 09:38 PM
|
|
Registered User
|
|
Join Date: Oct 2005
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|
 |