简体   繁体   中英

perform a search from multiple database tables, using php

I have registered an office that has multiple treatment, facility and courses (the values of treatment, facility and course was selected from treatment_type, office_facility and course table respectively) under it. For each item selected by each office I have created a separate table as it had many to many relation. The tables are:

register_office (used while registering office)

id  officename  city
1    Office-1    xyz
2    Office-2    xyz
3    Office-3    xyz

course_for_office (used when an office selects a course AND officeid is the id that was created while registering office)

id  officecourse  officeid
1     C1           1
2     C2           2

facility_for_office (used when an office selects a facility AND officeid is the id that was created while registering office)

id  officefacility  officeid
1     F1             1
2     F2             3

treatment_for_office (used when an office selects a treatment AND officeid is the id that was created while registering office)

id  officetreatment  officeid
1     T1              1
2     T2              2

Now in the front end i have 3 dropdown that are getting populated from database tables (treatment_type, office_facility, course). when the user selects each value i want that user should be able to see the list of offices and all their details (values such as officename, city etc will come from register_office table)

At present the code that i am using for search is

<?php
$con=mysqli_connect("localhost","root","","db");// Check connection
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$officetreatment = mysqli_real_escape_string($con, $_POST['officetreatment']);
$officecourse = mysqli_real_escape_string($con, $_POST['officecourse']);
$officefacility = mysqli_real_escape_string($con, $_POST['officefacility']);

$sql1 = "SELECT course_for_office.officecourse,
                facility_for_office.officefacility,
                treatment_for_office.officetreatment
                register_office.id
        FROM course_for_office INNER JOIN facility_for_office INNER JOIN treatment_for_office INNER JOIN
        ON course_for_office.officeid = facility_for_office.officeid = treatment_for_office.officeid = register_office.id
        WHERE officecourse LIKE '%$officecourse%' officetreatment LIKE '%$officetreatment%' officefacility LIKE '%$officefacility%'";


$result = mysqli_query($con, $sql1);

if (mysqli_num_rows($result) > 0) 
    {
        while($row = mysqli_fetch_assoc($result)) {
        echo  $row["officeid"];
    }
} else {
    echo "0 results";
}
mysqli_close($con);
?>

The problem is whenever I try to search I am getting 0 result even if the value exists in the database.

you just need to change your mysql query, try this one it will work...

SELECT cfo.officecourse,ffo.officefacility,tfo.officetreatment,ro.id,ro.officename
FROM register_office as ro
LEFT JOIN course_for_office as cfo ON ro.id = cfo.officeid
LEFT JOIN facility_for_office as ffo ON ro.id = ffo.officeid
LEFT JOIN treatment_for_office as tfo ON ro.id = tfo.officeid
WHERE cfo.officecourse LIKE '%$officecourse%'  
OR  tfo.officetreatment LIKE '%$officetreatment%' 
OR ffo.officefacility LIKE '%$officefacility%'

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