Skip to main content
Known Participant
Jun 17, 2010 2:32 PM UTC
Question

Filter Recordset by More Than One Field

I have an index page with a dynamic drop-down menu where you select the  state. Then you hit submit and it goes to my results page where a  dynamic table displays all record from my table with the selected state. This works perfectly.

Now, I want to add a second field to filter the recordset: year. The user shoul dbe able to select the state (ex: CA) and the year (ex:2006), hit submit and get their results. I just can't figure out how to get this to work.

On the index page, I added the second drop-down menu and have it displaying the distinct year values.

On the results page, I opened the recordset that the results are coming from. This was the code for just the first filter field:

SELECT *
FROM fiber_intake
WHERE `State` = colname

This worked just fine. The variable colname had this info associated with it:

Name: colname

Type: Text

Default value: -1

Run-time Value: $_GET['state']

I tried to write add the code for the second field, but I'm obviously doing in wrong (I'm not too familiar with MySQL):

SELECT *
FROM fiber_intake
WHERE `State` = colname  AND WHERE `Year` = colname2

I had defined a second variable colname2, even though I'm not sure if I was supposed to. Here's the info associated with colname2:

Name: colname2

Type: Numeric

Default value: -1

Run-time Value: $_GET['year']

When I viewed this in the browser my index page appears with my drop-down menus. I selected a year and hit submit. This is the error code I got:

You have an error in your SQL syntax; check the manual that corresponds  to your MySQL server version for the right syntax to use near 'WHERE  `Year` = 2006 LIMIT 0, 10' at line 1

Can anyone help me out?

Thanks!

This topic is closed to new replies. Start a new post to keep the conversation going.

7 replies

Participating Frequently
Jun 17, 2010 2:40 PM UTC

Only use the Where keyword once in the Where clause:

WHERE `State` = colname  AND `Year` = colname2

Translate
Known Participant
Jun 17, 2010 2:49 PM UTC

Ah, thanks for that bregent.

Now, I no longer get that error message, but it still doesn't work.

When I select a year, and hit submit it returns empty results.

I also tried to just select a state and now that returns empty reults too.

The info is being passed to the url (ex: http://10.100.136.29/csqConnect/results.php?state=&year=2006&submit=Submit) but it seems like it's just not getting through to the recordset.

Any suggestions?

Translate
Participating Frequently
Jun 17, 2010 3:11 PM UTC

Look at your querystring and see that the value for state is blank; so the problem is in the form page. You might want to paste the code for that page here.

Translate