I'm writing a small PHP/MySQL solution for keeping track of our lab's primate DNA samples. This consists of 2 SQL tables: the primate DNA table (ie Gorilla gorilla) and the organization where we got it table (ie San Diego Zoo). We deal with a number of organizations that send us DNA from the same species, so we designed an 'add sample' form where when you enter in a new sample, a drop down menu is used to call the organization table. This saves having to type the name of organization and keeps consistency when you need to do a search for all samples sent by that organization. The drop down menu does not insert the name of the organization into the 'add sample' submit, rather it puts the 'id' value of the organisation. This is useful because if the organization changes its' name, you only have to change the name, but not the ids. Thus your samples will still refer to the id of the organization and won't need to be changed.
We also have an 'edit sample' form as well, for the inevitable case when someone enters in incorrect information. In this case, the information is already in the samples table and we just call it into the form. In the case of the organization drop down menu, the id is submitted to the organization table and the corresponding name is returned to the drop down menu. When you click submit, the id of the organization is submitted to the samples table.
Prior to register_globals being turned off by default, all of the code in the 'edit sample' form worked fine.
This example of the code shows the dynamic drop down menu from the 'edit sample' form in question. So say for example the DNA sample gorilla has an organization id of '1', then the menu will translate that to the corresponding organization name from the organization table. In my case organization id ='1' translates to 'San Diego Zoo'. PHP will then display the name of the organization in the drop down menu rather than the id.
Code: Select all
<tr>
<td valign="top"><font size="-1" face="Verdana, Arial, Helvetica, sans-serif">Organization</font></td>
<td>
<select name="org">
<option value=""><unspecified></option>Code: Select all
<?php
// get organization list
$query = "SELECT id, name from organization ORDER BY id";
$result = mysql_db_query($db, $query, $connection) or die ("Error in query: $query. " . mysql_error());
while (list($id, $org) = mysql_fetch_row($result))
{
echo "<option value=$id";
if ($id == $name)
{
echo " selected";
}
echo ">$org</option>";
}
mysql_free_result($result);
?>Code: Select all
<?php
// get organization list
$query = "SELECT id, name from organization ORDER BY id";
$result = mysql_db_query($db, $query, $connection) or die ("Error in query: $query. " . mysql_error());
while (list($id, $org) = mysql_fetch_row($result))
{
echo "<option value=$id";
if ($_GET['id'] == $name)
{
echo " selected";
}
echo ">$org</option>";
}
mysql_free_result($result);
?>As I'm a scientist and not a programmer, forgive the lameness of the code.
I've manualed, googled and forum searched this one to death, so any insights into what I'm mucking up would be greatly appreciated.
Thanks in advance,
Luke Kirkwood