You can't.
You'll need to dynamically generate the conditions (everything after WHERE) based on what you have:
PHP Code:
$conditions = array('date','employee','location');
foreach($conditions as $c) {
if isset($$c) {
$condition[] = $c."='".$$c."'";
}
}
$c = isset($c)?'WHERE '.implode(' AND ',$c):'';
$query = 'blah blah blah'.$c.';'; //becomes BLAH BLAH BLAH WHERE X=...........;
That's messy, but I think it will work.
It uses the kinda ugly $$ format which references a dynamically named variable (or variable variable):
$a = 'b';
$b = 1;
echo $$a; //echos '1'
The best way to redesign all of this is to deal with the information already in an array then use implode like I did above. Simpler, but that should patch what you currently have.
By the way, I set it up to handle nothing sent so you don't ahve a where statement at all. You could also refuse the query if no value was sent.
Note also that I added a semicolon to end the query. While this isn't required for one-statement MySQL queries, it's a good idea for security reasons: it helps ensure that no one is injecting something malicious at the end of the query. (It's not foolproof, but it helps along with other things like escaping the data.)
Bookmarks