The following code gives me a drop down-list of values from a table.
<?
// includes
include("../includes/config.php");
include("../includes/functions.php");
// open database connection
$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");
// select database
mysql_select_db($db) or die ("Unable to select database!");
// generate and execute query
$query = "SELECT * FROM RaceResults WHERE RaceID = '$RaceID'";
$get_raceresults_result = mysql_query($query) or die ("Error in query: $get_raceresults_query. " . mysql_error());
function ListboxMatchState ($size, $name, $query, $matchtothis) {
$qry = mysql_query($query);
if (mysql_num_rows($qry) > 0)
{
echo "<SELECT SIZE ='".$size."' NAME='".$name."'>\n\t<OPTION
VALUE=''>\n";
while (list($StateID, $StateName) = mysql_fetch_row($qry))
{
echo "\t<OPTION VALUE='".$StateID."'";
if ($StateID == $matchtothis) { echo " SELECTED"; }
echo ">".$StateName."\n";
}
echo "</SELECT>";
}
else echo "No data in listbox\n";
mysql_free_result($qry);
} / end ListboxMatchState /
function ListboxMatchCity($size, $name, $query, $matchtothis) {
$qry = mysql_query($query);
if (mysql_num_rows($qry) > 0)
{
echo "<SELECT SIZE ='".$size."' NAME='".$name."'>\n\t<OPTION
VALUE=''>\n";
while (list($CityID, $CityName) = mysql_fetch_row($qry))
{
echo "\t<OPTION VALUE='".$CityID."'";
if ($CityID == $matchtothis) { echo " SELECTED"; }
echo ">".$CityName."\n";
}
echo "</SELECT>";
}
else echo "No data in listbox\n";
mysql_free_result($qry);
} / end ListboxMatchCity /
?>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body bgcolor="#FFFFFF" text="#000000">
<form action="do_modstate.php" method="post">
<table>
<tr>
<td>Race ID:</td>
<td><input type="text" value="<? echo $RaceID; ?>"></td>
<TR VALIGN="TOP">
<TD ALIGN="RIGHT"><B>State:</B></TD>
<TD><?php ListboxMatchState(1,"StateID", "SELECT StateID,
StateName FROM State GROUP BY StateID",
$StateID); ?> </TD>
</TR>
<TR VALIGN="TOP">
<TD ALIGN="RIGHT"><B>City:</B></TD>
<TD><?php ListboxMatchCity(1,"CityID", "SELECT CityID,
CityName FROM City GROUP BY CityID",
$CityID); ?> </TD>
</TR>
<tr>
<td>
<input type="submit" name="submit" value="submit"></td>
</tr>
</table>
</form>
</body>
In this case the URL would be something like http://localhost/admin/listbox.php?RaceID=1&CityID=2&StateID=3
When I click on submit the following code is run:
<?
// do_modstate.php
?>
<?
// includes
include("../includes/config.php");
include("../includes/functions.php");
// open database connection
$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");
// select database
mysql_select_db($db) or die ("Unable to select database!");
// create SQL statement
$update_state_sql = "UPDATE RaceResults SET StateID = '$StateID', CityID='$CityID' WHERE RaceID = '$RaceID'";
// execute SQL query and get result
$update_state_sql_result = mysql_query($update_state_sql,$connection)
or die(mysql_error());
?>
I get an error that
Warning: Undefined variable: RaceID in E:admin\do_modstate.php on line 23
Where line 23 is:
$update_state_sql = "UPDATE RaceResults SET StateID = '$StateID', CityID='$CityID' WHERE RaceID = '$RaceID'";
If I set the RaceID in the URL I get an error that the CityID is undefined and if I add the CityID I get and error that StateID is undefined.
All I want to do is update the table with the changed values in the drop-down list.
Any suggestions/