By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
426,060 Members | 1,878 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 426,060 IT Pros & Developers. It's quick & easy.

PHP/MySQL: query based on selected form option

P: n/a
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?

Jul 17 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
JDJones wrote:
(snip)
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';
## set $query to false if no category found
$query = false;
}
}

## if there was a category entered
if ($query) {
$results=mysql_query($query);
$numrows=mysql_num_rows($results);

etc etc etc
## end if
}

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?


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 =--
Jul 17 '05 #2

P: n/a
Pedro Graca wrote:
JDJones wrote:
(snip)
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';

## set $query to false if no category found
$query = false;

}
}

## if there was a category entered
if ($query) {

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

etc etc etc

## end if
}

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?

Try the three lines I inserted in your code above.


Thank you Pedro. That worked but you knew that, right? ;)

Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.