Check box and sql string

Hello folks,

Now here’s a thing.I have an sql SELECT string that has three “variables”.Each variable takes it’s value from a Listbox.
So the SQL string looks like this:
SELECT * FROM (Table) where field 1 = ‘" variable1’" AND field 2 = ‘"variable2’" AND field 3 = ‘"Variable3’"

So when an item is clicked in either of the three boxes it finds it’s way to the variable and completes the SQL string.The problem is when I click only two of the boxes the query doesn’t work and returns nothing.
Is there any ay around this.I have also tried the same approach with checkboxes but get the same response,I miss out a box and the string returns nothing.

Any help appreciated.

thanks
inatree

I will assume that variable1, 2 etc are only available if selected, so query should be built to check for that.

<?php
/////////////////////
//sample data
$variable1 = "Dog";
$variable2 = "Large";
$variable3 = "Black";
/////////////////////

$sql = "SELECT * FROM (Table) ";

if(isset($variable1) || isset($variable2) || isset($variable3)):
	$sql .= "WHERE ";
endif;
$presql = array();
if(isset($variable1)):
	$presql[] = "field_1 = '" . $variable1 . "'";
endif;
if(isset($variable2)):
	$presql[] = "field_2 = '" . $variable2 . "'";
endif;
if(isset($variable3)):
	$presql[] = "field_3 = '" . $variable3 . "'";
endif;

if(!empty($presql)):
	$sql .= implode(" AND ",$presql);
endif;

echo $sql;
//SELECT * FROM (Table) WHERE field_1 = 'Dog' AND field_2 = 'Large' AND field_3 = 'Black'
?>