1

I am building my first PHP/MySQL site and i would like to return search results from two fields in my one table within the database - Catagory, and Location (There are 15 fields in total in the table, but there are only two required for the search).

Each option will have a drop down menu which will list a fixed number of choices in each field - for instance, there will probably be 15 or so Locations, and 7 or 8 Catagories.

I want the user to be able to select an item from either one or both drop down menus, click a 'Search' or @Go' button, and be presented with the results.

As mentioned, i am brand new to PHP/MySQL so apologies if my question is a little 'simple' and i hope someone can point me in the right direction.

Thanks Dan

EDIT:

Brad, Conner, I don't have anything thus far i'm afriad in terms of the search query specifics, i'm just starting to research how to do it hence complete lack of code in my question!

But to try and be more specific, the only fields i want to return results from are Location and Job Catagory - these are two fields of about 15 in my table that are already populated with fixed terms - e.g, Location may have London, Manchester, Leeds, and Catagory may have Administrative, Management, Clerical etc - there is no option for the visitor to input a new term, only select from a drop down menu of pre-arranged items.

The other fields in my table aren't relevant to the search (salary, company, contact email etc), so...I am thinking i only need two queries, and results can also be displayed if you only send one (e.g, you aren't bothered about job location, just speciality, you could still find all the results and decide if you fancied relocating). Is that any better or more helpful?

Thanks for you help so far. Dan

3
  • Dan, the problem with your question isn't of simplicity, but that there isn't enough information. Can you show us your table schema and what queries you are using thus far? Also, please read the FAQ here: stackoverflow.com/faq Thanks.
    – Brad
    Jun 16, 2011 at 21:01
  • You want to return all Location and Job Categories no matter what? Or do you have a conditional, like get all Locations and Job Categories where Location is in Texas? Jun 16, 2011 at 21:11
  • Sorry for lack of specifics, its because i actually don't have that many yet. Edited above to be more helpful hopefully.
    – Dan
    Jun 16, 2011 at 21:16

3 Answers 3

1

A user enters a search query and you'd like to select the rows from your table that contain or exactly match that query? If so, something like

For if category contains $search_query

SELECT category, location 
FROM table 
WHERE concat(' ', category, ' ') LIKE '%$search_query%'

Or if it is an exact match you would use this for your conditional instead:

WHERE category = '$search_query'

I'm probably offtrack though, you need to specify like Brad said

3
  • Hi, i'd rephrase your intro to "A user SELECTS a search query and you'd like to select the rows from your table that contain or exactly match that query?" Thanks, Dan
    – Dan
    Jun 16, 2011 at 22:11
  • Okay I thought maybe you had a search bar I didn't know everything was pre-populated. Let me know if you need any more help and I'll edit my answer :) Jun 17, 2011 at 3:04
  • Thanks Connor, i'll play around with what you've suggested and will probably come crawling back shortly. Thanks again,
    – Dan
    Jun 17, 2011 at 19:11
1

Dan you need to build your sql statement dynamically. Below is a simple peice of code to demonstrate what I mean. Always besure to escapse user data to provent sql inject.

if ( isset($_POST['go']) ){
      $location = $_POST['location'];
      $category = $_POST['category'];
      $sql = "SELECT * FROM table ID IS NOT NULL ";

      if ( $location !="" ){
           $sql .= " AND location = " . $location;
      }

      if ( $category !="" ){
           $sql .= " AND category = " . $category;
      }

}
5
  • You can also use !empty($location) and !empty($category) Jun 16, 2011 at 21:20
  • Hi Sid, i don't think i need to escape anything as the user doesn't have a chance to input anything, they can only select from one or both of two drop down lists.
    – Dan
    Jun 16, 2011 at 21:39
  • If you are passing 0 as location id or category id then you cannot use the empty function as the empty function will evaluate 0 as being empty. This is just for though, if adding other parameters that require a 0. Jun 16, 2011 at 21:49
  • @Dan, is this an authenticated page? if not, a user can save the page as html onto their machine change the value in the dropdown menu and post the form back to your server with the modified values. And if that wasn't a hackers choice s/he could just create a php script of their own and post data using the same form fields to your server. Jun 16, 2011 at 21:55
  • no it probably isn't! I'm brand new to PHP/MySQL Sid, i built my database and got my scripts talking to it yesterday so i'm a little green to say the least. Thanks for your help and the heads up. Dan
    – Dan
    Jun 16, 2011 at 22:00
0

Without any details on your table structure and the data in question, the basics would be somethign like:

SELECT ...
FROM ...
WHERE (field1 = $location_option) or (field2 = $category_option)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.