tags:

views:

316

answers:

2

Hi.

I have a two tables:

  1. Clients
  2. Cities

Then in the search form I have three fields:

  1. A text input form to fill with the name of the client
  2. A select box to choose the city where they want to search(table cities)
  3. Another select box to choose the main business that the user wants to search for(businness is included in the clients table)

Depending on the choices of the form field it will make a search from the two tables.

Im thinking in doing multiple SQL querys depending on the user choices.

The problem is that maybe the user will not fill the input form with the name of the client, or maybe it will, but will not choose a city, etc. etc...

So will I have to do multiple ifs to show multiple sql querys depending on user choice? maybe i have a less confusing solution...

Thanks.

+2  A: 

Perhaps your database structure could be altered to help you. For example, I assume you have a relationship between Cities and Clients. It sounds like Businesses is something like "Tech" or "Search" in which case could you have multiple clients with the same Business? Could be worth porting businesses to a separate table and then referring to those. Then you can have a simple query such as:

SELECT *
FROM clients
WHERE clients.city = {form result: cities.id}
AND clients.business = {form result: businesses.id}
ORDER BY clients.name
LIMIT 0, 30

Now assuming the user does not fill in one of the fields (e.g. city) you have two choices: force them to fill it in by validating user input or use it as a wildcard, in which case you could remove the WHERE clients.city clause from your query. Assuming you're giving your <select> options values of an id from Cities.

Programmatically I'd build the query in this case:

$queryStr = 'SELECT * FROM clients';
if (!empty($_POST['city']) && !empty($_POST['business'])) {
    $queryStr .= ' WHERE city = ' . filter($_POST['city']) . ' AND business = ' . filter($_POST['business']);
} elseif (!empty($_POST['city'])) {
    $queryStr .= ' WHERE city = ' . filter($_POST['city']);
} elseif (!empty($_POST['business'])) {
    $queryStr .= ' WHERE business = ' . filter($_POST['business']);
} else {
    // pass
}
$queryStr .= ' ORDER BY name LIMIT 0, 30';

Where filter is your choice of filtering method.

Ross
The business table its already made but its not connected to the clients table. My client gave me an access database to use, but its very old and not well made. And in the clients table there is a field with business too. There are 500 businesses. How can i convert the name of the business to the id? Make an excel and do convertions?I have to do one by one?
dutraveller
I've not done a lot of work with Access (I'd much rather work with mysql/pgsql - it might be easier for you to dump tables as csv and attempt to import them). Rather than use the name of each business as an id I'd give each one an identification number (AutoNumber in Access) and refer to them by that.
Ross
+1  A: 

Simply query the DB with your criteria. If clients/cities actually exist, then they'll show up, otherwise the query will return an empty set. Easy as that, no need to validate the existence of either of them - the user should be smart enough to be able to come up with an existing city name for example.

The only thing you need to do is to mysql_real_escape_string().

Flavius
mysql_real_escape_string() in the $_POST['variable'] right?
dutraveller
yes(padding to at least 10 chars)
Flavius