Hi guys, im strugling with building a complex search query.
I have a table database with 8 fields in one table. None of the fields are complsary.
On the search form the user can fill in any/all of the 8 fields to build their search.
My problem is that for any field that the user does not input a search keyword into, instead of the search query realisin that the user does not know data for that field it presumes there is no data for the blank fields.
Basically I need a search query that works like the following below (the below does not work and I realise it shouldnt, but you will see how I need it to work:
<?php
$GetContact = "SELECT * FROM contacts LEFT JOIN schools ON con_sch_id = sch_id WHERE con_client_id = '$client' AND
if ($keyword1) {
con_firstname LIKE '%".$keyword1."%' AND
}
if ($keyword2) {
con_lastname LIKE '%".$keyword2."%' AND
}
if ($keyword3) {
con_school LIKE '%".$keyword3."%' AND
}
if ($keyword4) {
con_age LIKE '%".$keyword4."%' AND
}
if ($keyword5) {
con_form LIKE '%".$keyword5."%' AND
}
if ($keyword6) {
con_loe LIKE '%".$keyword6."%' AND
}
if ($keyword7) {
con_town LIKE '%".$keyword7."%' AND
}
if ($keyword8) {
con_city LIKE '%".$keyword8."%'
}
ORDER BY con_lastname asc, con_firstname asc";
$ResultContact = mysql_query($GetContact);
$TableRows = mysql_num_rows($ResultContact);
?>