简体   繁体   中英

PDO advanced search from multiple tables

I am trying to create a person search, based on multiple select boxes , radio buttons and dropdowns .

Some of the select boxes are arrays so I need to consider all of the selected options to show the results.

There are different tables for services, languages, about, work experience etc.

But something is wrong with my logic. At the moment, when I am trying the search it goes like this: I select English language from the language dropdow, but there is nobody who speaks English so there are no results which is correct. Then if I select one of the services (let's sat cleaning), then there will be one result because the same person has listed cleaning as their service, but this is wrong because he doesn't speak English.

I am using union all and LIKE in my query, but can somebody tell me why it still showing results? If i am doing something wrong, could you please point me in the right direction?

This is my query and PHP code so far:

if (isset($_POST['userServices']) && !empty($_POST['userServices'])){
    $userServices = $_POST['userServices'];
    for ($i = 0; $i < count($userServices); $i++) {
       $services = $userServices[$i];
    }
}
if (isset($_POST['languagesArray']) && !empty($_POST['languagesArray'])){
    $languagesArray = $_POST['languagesArray'];
    for ($i = 0; $i < count($languagesArray); $i++) {
        $languages = $languagesArray[$i];
    }
}
$search = $user_home->runQuery("

    SELECT nanny_services.user_id, userFirstName 
    FROM hoidja.nanny_services 
    JOIN services on nanny_services.service_id = services.service_id 
    JOIN tbl_users ON tbl_users.user_id = nanny_services.user_id 
    WHERE (services.service_id LIKE :service_id) 
    GROUP BY nanny_services.user_id;

    UNION ALL

    SELECT user_language.user_id, userFirstName 
    FROM hoidja.user_language 
    JOIN languages on user_language.user_language_id = languages.user_language_id 
    JOIN tbl_users ON tbl_users.user_id = user_language.user_id 
    WHERE (languages.user_language_id LIKE :language_id) 
    GROUP BY user_language.user_id

    ");
$search->execute(array(
    ':service_id'     => $services, 
    ':language_id'    => $languages, 

));
$search_results = $search->fetchAll(PDO::FETCH_ASSOC);  
    echo json_encode($search_results);

Something like this

SELECT nanny_services.user_id, userFirstName 
FROM hoidja.nanny_services 
JOIN services on nanny_services.service_id = services.service_id 
JOIN tbl_users ON tbl_users.user_id = nanny_services.user_id 
WHERE (services.service_id = :service_id) 
AND (languages.user_language_id = :language_id)
GROUP BY nanny_services.user_id;

without UNION

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM