I'm sorry I am not being clear. I will try again...
The search results should bring back all profiles with the matching criteria. Only approved, and gender must be as below:
approved = Yes
gender = Male/Female (based on $SameGenderCheck;, males cannot search for males, and females cannot search for females)
age should be a number between 16 and 65 selected via two dropdowns ($age_from and $age_to)
I want the user to select the age and the city as required fields, the other search options are optional so if they are entered they will further narrow down the search results if they are not entered then just use age and city. The gender will always be the opposite of the user. And I only want the user to see the people who have been approved so "approved" has to be set to Yes
There is nothing else.
Thank you in advance
>the other search options are optional so if they are entered they will further narrow
>down the search results if they are not entered then just use age and city.
OK, I believe this is actually the part that is causing problems for you. You want all of the criteria to NARROW your results. You want ethnicity, nationality, marital_history and height to be optional criteria, so you've combined them with the rest of the statement with an OR. But an OR condition will always widen the results, not narrow. So you must change the first OR to an AND. The OR's used inside the parens for those criterial are OK.
$query_search_res = "SELECT * FROM profiles LEFT JOIN profile_details ON profiles.profile_id = profile_details.profile_id WHERE (city = '$location') AND (ethnicity = '$ethnicity' OR nationality = '$nationality' OR marital_history = '$marital_history' OR height = '$height') AND (Age BETWEEN '$age_from' AND '$age_to') AND (gender != '$SameGenderCheck') AND (approved = 'Yes')";
But now comes the problem; if the user has no entries for the ethnicity, nationality, marital_history and height criteria, no results will be returned. Plain SQL will not solve your problem. You can do a workaround by changing the equality operator to the LIKE predicate and putting a wildcard for users that don't have this criteria in their profile. But that only works for text fields, not numeric, and I don't like it.
A better solution is to dynamically create the WHERE clause, removing the ethnicity, nationality, marital_history and height criteria from the WHERE clause if the user doesn't have these fields completed.