473,378 Members | 1,084 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

PHP/MySQL: query based on selected form option

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
2 5666
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: iwasinnihon | last post by:
I don't usually like to do this, but I need help. I have gone over this code and cannot figure out why it doesn't work. First of all it doesn't check to see if you have filled in the required...
7
by: Ivan Marsh | last post by:
Hey Folks, I'm having a heck of a time wrapping mind around AJAX. Anyone know of a simple, straight-forward example for pulling a simple query from mysql with PHP using AJAX? As I...
1
by: mpar612 | last post by:
Hi everyone, I'm not sure if this is asking too much or not. I am trying to get the following PHP code to work on my website. It utilizes PHP 5, MySQL 4.1 and the PEAR DB module. I am...
2
by: Tarik Monem | last post by:
OK! I've gone through a few tutorials and I cannot understand what I'm doing wrong casting_registration.php <table> <tr> <td> <form enctype="multipart/form-data" action="thankyou.php"...
6
by: jej1216 | last post by:
I am trying to put together a PHP search page in which the user can select none, one, two, or three fields to search, and then the results php will build the SQL with dynamic where caluses to reflect...
11
by: patsman77 | last post by:
Hello All, I am having a frustrating time trying to figure this out. I have a form, in which I want to have fields WEEK, ID, FAVORITE, UNDERDOG, SPREAD, FSCORE, USCORE, and TIME. The form is...
4
by: TechnoAtif | last post by:
Hi ALL I have entered some array values using checkboxes into mysql database through a form. Next iam creating a searchpage where all those cateogories inserted through checkboxes has to be...
3
by: janetopps | last post by:
I have a news website, with asp pages, which was on Access, and i upgraded to MySQL, i used Bullzip to transfer the data. It had about 1000 pages, which im now able to pull up on the public side. Im...
1
by: matthewroth | last post by:
i have this script that works very well. Basically it allows the user to attach a scanned pdf file of shipping forms and attach to the form. <? include_once("format.php"); echo $format; ?>...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.