Using PHP and MySQL. Trying to put a list of categories into a drop down
select option of a form like:
<form name="form" action="<? print $_SERVER['PHP_SELF']?>" method="get">
<select name="subject">
<option value=""></option>
<option value="field1"> Field 1</option>
<option value="field2"> Field 2</option>
</select>
<input type="submit" name="Submit" />
</form>
Then I want to process it so the MySQL query gets done depending on what
was selected. I came up with this:
//connect to database
mysql_connect(" $dbhost","$dbus er","$dbpass" );
mysql_select_db ("$dbase") or die("Unable to select database");
// Build SQL Query
if (isset($_GET['subject']))
{
switch($_GET['subject'])
{
case 'field1':
$query = "select * from tips where category = 'field1'";
break;
case 'field2':
$query = "select * from tips where text like 'field2' ";
break;
default:
echo 'No subject found';
}
}
$results=mysql_ query($query);
$numrows=mysql_ num_rows($resul ts);
etc etc etc
But that switch doesn't seem to work. Anyone have a suggestion as to how
I can code this to do the MySQL query based on the subject?
select option of a form like:
<form name="form" action="<? print $_SERVER['PHP_SELF']?>" method="get">
<select name="subject">
<option value=""></option>
<option value="field1"> Field 1</option>
<option value="field2"> Field 2</option>
</select>
<input type="submit" name="Submit" />
</form>
Then I want to process it so the MySQL query gets done depending on what
was selected. I came up with this:
//connect to database
mysql_connect(" $dbhost","$dbus er","$dbpass" );
mysql_select_db ("$dbase") or die("Unable to select database");
// Build SQL Query
if (isset($_GET['subject']))
{
switch($_GET['subject'])
{
case 'field1':
$query = "select * from tips where category = 'field1'";
break;
case 'field2':
$query = "select * from tips where text like 'field2' ";
break;
default:
echo 'No subject found';
}
}
$results=mysql_ query($query);
$numrows=mysql_ num_rows($resul ts);
etc etc etc
But that switch doesn't seem to work. Anyone have a suggestion as to how
I can code this to do the MySQL query based on the subject?
Comment