modified to use prepared statements. Also simplified to use one query template for all search conditions