| re: PHP/MySQL: query based on selected form option
Pedro Graca wrote:[color=blue]
> JDJones wrote:
> (snip)
>[color=green]
>>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","$dbuser","$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';[/color]
>
>
> ## set $query to false if no category found
> $query = false;
>
>[color=green]
>>}
>>}
>>[/color]
>
>
> ## if there was a category entered
> if ($query) {
>
>[color=green]
>>$results=mysql_query($query);
>>$numrows=mysql_num_rows($results);
>>
>>etc etc etc[/color]
>
>
> ## end if
> }
>
>[color=green]
>>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?[/color]
>
>
> Try the three lines I inserted in your code above.[/color]
Thank you Pedro. That worked but you knew that, right? ;) |