[英]PHP advanced search with Multiple OPTION
I am building a search field with php where users can search for Doctors information with multiple search options. 我正在用php构建一个搜索字段,用户可以在其中使用多个搜索选项搜索Doctors信息。
As shown in the picture a user can search by: DR.NAME, SPECIALTY, DIVISION, LOCATION. 如图所示,用户可以通过以下方式搜索:DR.NAME,SPECIALTY,DIVISION,LOCATION。 The DR.NAME should match any keyword and the form doesn't require any fields to be filled out.
DR.NAME应该与任何关键字匹配,并且该表格不需要填写任何字段。
This is my current code which isn't working. 这是我当前的代码,无法正常工作。
doctorsearch.php doctorsearch.php
<?php
error_reporting(0);
include 'config.php';
$d_fname = $_POST['d_fname'];
$d_spcl = $_POST['d_spcl'];
$d_division = $_POST['d_division'];
$d_location = $_POST['d_location'];
$qry = "SELECT * FROM doctor_reg WHERE ";
if ($d_fname != '') {
$qry .= "d_fname='".mysql_real_escape_string($d_fname)."' AND ";
}
if ($d_spcl != '') {
$qry .= "d_spcl='".mysql_real_escape_string($d_spcl)."' AND ";
}
if ($d_division != '') {
$qry .= "d_division='".mysql_real_escape_string($d_division)."' AND ";
}
if ($d_location != '') {
$qry .= "d_location='".mysql_real_escape_string($d_location)."' AND ";
}
$result = mysql_query($result);
?>
<?php
echo "<table border='1px solid #CCCCCC;' width='100%'>";
echo "<tr style='color:#FFFFFF;background:#555555;'>";
echo "<th style='padding:3px;'>Name</th>";
while($row = mysql_fetch_array($result)){
echo "<tr class='trbd'>";
echo "<td style='padding:3px;'>".$row['d_fname'].' '.$row['d_lname']."</td>";
?>
<?php
echo "</tr>";
}
echo "</table>";
?>
You need to add OR
instead of AND
. 您需要添加
OR
而不是AND
。
Generally, when users search they search by OR
condition. 通常,当用户搜索时,他们通过
OR
条件进行搜索。
For example: Doctor Name should be Sharma or location should be east street. 例如:医生名称应为Sharma或位置应为东街。
If we search with AND
conditions, database will search only records who have the exact combination. 如果我们使用
AND
条件进行搜索,则数据库将仅搜索具有确切组合的记录。
AND
returns true
if all the conditions are true. AND
返回true
,如果所有条件都满足。
OR
returns true
if any of conditions is true. OR
返回true
,如果任何条件为真。
Therefore, OR
is correct syntax here. 因此,
OR
在这里是正确的语法。
Corrected code: 更正的代码:
$qry = "SELECT * FROM doctor_reg";
$searchArray = array();
if ($d_fname != '') {
$searchArray[] = "d_fname LIKE '%".mysql_real_escape_string($d_fname) . "%'";
}
if ($d_spcl != '') {
$searchArray[] = "d_spcl LIKE '%".mysql_real_escape_string($d_spcl) . "%'";
}
if ($d_division != '') {
$searchArray[] = "d_division LIKE '%".mysql_real_escape_string($d_division) . "%'";
}
if ($d_location != '') {
$searchArray[] = "d_location LIKE '%".mysql_real_escape_string($d_location) . "%'";
}
$qry .= ! empty($searchArray) ? " WHERE " . implode(" OR ", $searchArray) : '';
if you want any keyword not exact match then you shoud use like instead of = operator, so change this 如果您希望关键字不完全匹配,则应使用like代替=运算符,因此请更改此设置
if ($d_fname != '') {
$qry .= "d_fname='".mysql_real_escape_string($d_fname)."' AND ";
}
into this 进入这个
if ($d_fname != '') {
$qry .= "d_fname LIKE'%".mysql_real_escape_string($d_fname)."%' AND ";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.