Connecting Tech Pros Worldwide Help | Site Map

PHP/MySQL: query based on selected form option

JDJones
Guest
 
Posts: n/a
#1: Jul 17 '05
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","$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';
}
}

$results=mysql_query($query);
$numrows=mysql_num_rows($results);

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?

Pedro Graca
Guest
 
Posts: n/a
#2: Jul 17 '05

re: PHP/MySQL: query based on selected form option


JDJones wrote:
(snip)[color=blue]
> 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=blue]
> }
> }
>[/color]

## if there was a category entered
if ($query) {
[color=blue]
> $results=mysql_query($query);
> $numrows=mysql_num_rows($results);
>
> etc etc etc[/color]

## end if
}
[color=blue]
>
> 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.


Also indent your code (even small scripts like this one!) so that you
can much more easily catch eventual errors.


compare with

foreach ($arr as $k=>$v) foreach ($arr as $k=>$v)
{ {
if ($k = 'name') if ($k = 'name')
{ {
$b = '<b>' . $b . '</b>'; $b = '<b>' . $b . '</b>';
$v = '<b>' . $v . '</b>'; $v = '<b>' . $v . '</b>';
} }
echo "$k = $v<br/>\n"; echo "$k = $v<br/>\n"
} }
--
--= my mail box only accepts =--
--= Content-Type: text/plain =--
--= Size below 10001 bytes =--
JDJones
Guest
 
Posts: n/a
#3: Jul 17 '05

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? ;)

Closed Thread