我已经在其下注册了一个办公室,该办公室有多个治疗,设施和课程(治疗,设施和课程的价值分别从treatment_type,office_facility和course table中选择)。对于每个办公室选择的每个项目,我创建了一个单独的表,因为它有多对多的关系。表格是:
register_office(在注册办公室时使用)
id officename city
1 Office-1 xyz
2 Office-2 xyz
3 Office-3 xyz
course_for_office(办公室选择课程时使用AND officeid是注册办公室时创建的ID)
id officecourse officeid
1 C1 1
2 C2 2
facility_for_office(在办公室选择设施时使用,并且officeid是在注册办公室时创建的ID)
id officefacility officeid
1 F1 1
2 F2 3
treatment_for_office(办公室选择治疗时使用AND officeid是注册办公室时创建的ID)
id officetreatment officeid
1 T1 1
2 T2 2
现在在前端我有3个下拉列表,从数据库表中填充(treatment_type,office_facility,course)。当用户选择我希望该用户应为able to see the list of offices
的每个值时,他们的所有细节(诸如officename,city等值将来自register_office表)
目前我用于搜索的代码是
<?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);
?>
问题在于,每当我尝试搜索时,即使数据库中存在该值,我也会得到0结果。
答案 0 :(得分:4)
你只需要更改你的mysql查询,试试这个就可以了......
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%'