$query = "select * from table where";
your Initial SQL querry
now, we check if the data was filled.
if it was, we add pieces to the querry, acording to what was filled.
if($name != null)
{
$query .= " name = '$name'";
$and = " and";
}
$query = "select from table where";
$query .= " name = $name";
is the same as
$query = "select from table where name = '$name'";
it´s not that hard