PHP/MySQL Drop Down Menu Form question
Posted: Sun Mar 23, 2008 3:15 pm
Hello:
I'm new to PHP. I've been reading the PHP & MySql documentation, plus trying the W3C School tutorials for about a week now and I still can't figure out how to write PHP code to populate a drop down menu form via a MySQL db. Any pointers/examples/suggestions much appreciated. In my example I believe this is a (2) step process:
1) Writing the PHP code to populate the drop down menu from a MySQL db.
2) Writing the PHP code that processes what the user selects from the drop down menu (the form action PHP script).
Please correct me if I'm wrong about the process above.
Step 1...here is the code I have so far for populating a drop down menu:
//Making the db connection
<?php
$con = mysql_connect("my_server","uid","pw");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("my_db", $con);
//Select all baseball player image captions from a MySQL db table
$result = mysql_query("SELECT caption
FROM tbllinkcat, tblimages
WHERE tbllinkcat.catid = tblimages.catid
AND tbllinkcat.catid=3");
//List all baseball players in a form drop down menu
while($row = mysql_fetch_array($result))
***And here is where I'm stuck. I'm not sure what to put after the while statement above to correctly place the fetched records in the array inside a drop down box. The MySQL select statement above works (in phpMyAdmin), and, I've seen examples of writing an of array records in an html form, using echo to call the form method and action, etc. but most of the examples don't make sense to me***
//Close db connection
mysql_close($con);
?>
Step 2...process the record that the user selects from the drop down box. I have written the MySQL select statements, but I'm uncertain how to write the PHP code that runs the SQL statements based on which record the user selects from the drop down box. For instance, if the user selects Ty Cobb from the drop down box I want to run a SQL query that will create a html page that displays all the Ty Cobb db records (images). Or if the user selects Babe Ruth from the drop down box I'll run a different query to display all the Ruth db records, etc.
Again...any examples/comments/suggestions much appreciated. Thanks!
I'm new to PHP. I've been reading the PHP & MySql documentation, plus trying the W3C School tutorials for about a week now and I still can't figure out how to write PHP code to populate a drop down menu form via a MySQL db. Any pointers/examples/suggestions much appreciated. In my example I believe this is a (2) step process:
1) Writing the PHP code to populate the drop down menu from a MySQL db.
2) Writing the PHP code that processes what the user selects from the drop down menu (the form action PHP script).
Please correct me if I'm wrong about the process above.
Step 1...here is the code I have so far for populating a drop down menu:
//Making the db connection
<?php
$con = mysql_connect("my_server","uid","pw");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("my_db", $con);
//Select all baseball player image captions from a MySQL db table
$result = mysql_query("SELECT caption
FROM tbllinkcat, tblimages
WHERE tbllinkcat.catid = tblimages.catid
AND tbllinkcat.catid=3");
//List all baseball players in a form drop down menu
while($row = mysql_fetch_array($result))
***And here is where I'm stuck. I'm not sure what to put after the while statement above to correctly place the fetched records in the array inside a drop down box. The MySQL select statement above works (in phpMyAdmin), and, I've seen examples of writing an of array records in an html form, using echo to call the form method and action, etc. but most of the examples don't make sense to me***
//Close db connection
mysql_close($con);
?>
Step 2...process the record that the user selects from the drop down box. I have written the MySQL select statements, but I'm uncertain how to write the PHP code that runs the SQL statements based on which record the user selects from the drop down box. For instance, if the user selects Ty Cobb from the drop down box I want to run a SQL query that will create a html page that displays all the Ty Cobb db records (images). Or if the user selects Babe Ruth from the drop down box I'll run a different query to display all the Ruth db records, etc.
Again...any examples/comments/suggestions much appreciated. Thanks!