Forum Moderators: open

Message Too Old, No Replies

Searching MySql db

How do you seperate possible searches

         

wheelie34

8:44 am on Oct 27, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi all

Its fairly straight forward to search a db when theres only one possible way the user can search, what about when you have, lets say, 3 selects and a tick box?

How do you build a function for it? whats the correct method?

I have, for example, the following search facility, the first 3 are select boxes, last is a tick box.

location (where the user prefers)
start (when to start the holiday)
amount (how many people)
show part available (tick box can be ticked or left unticked)

The user, through javascript, MUST select start, but could select just that OR start & location, OR start, location & amount, or all PLUS the tick box, what road map do I need to follow to handle all possabilities?

[edit]using php[/edit]

Thanks as always

Romeo

12:58 pm on Oct 27, 2006 (gmt 0)

10+ Year Member



Hi Wheelie,

You just have to put your logic into some IFs, ANDs or ORs ...
Perhaps going like this ...

<?php

# validate all input variables
...

# prepare the SQL SELECT statement

$sql = '';

if (isset($start)) {
$sql = "select something from table where start='$start'";
if (isset($location)) {
$sql .= " and location='$location'";
}
if (isset($amount)) {
...
}
...
} # end IF start

# execute the $sql
...
?>

HTH and kind regards,
R.

wheelie34

8:08 pm on Oct 27, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



is it possible to use

if (isset($whatever)){
$sql .= " AND whatever ='$whatever'";
}
2 or 3 times? if so, how? surely if theres 3 possible search types, I will need to run multpile if's

Romeo

8:40 pm on Oct 31, 2006 (gmt 0)

10+ Year Member



Yes, it is possible, and a second 'IF' is already there, those '...' just need to be expanded with more code. No problem, to add another one, if you wish.

Be aware, that it is important to validate your input variables before you start using them, otherwise you are at risk of your script being hacked, your data being stolen or damaged or compromised, and your server being 0wned (SQL injection).

Kind regards,
R.