多字段PHP搜索

多字段PHP搜索,php,mysql,search,Php,Mysql,Search,我想使用带有多字段搜索选项(例如姓名、学院、系、年份、国籍等)的ajax方法搜索数据。我有一个用于搜索的插入名称,其余字段比foreach循环中的字段为空,但此if(isset($\u GET[$field])&&!empty($\u GET['$field]])条件未成功并转到else循环 $fields = array( 'name' => TRUE, 'gender' => TRUE, 'colf' => TRUE, 'deptf' => TRU

我想使用带有多字段搜索选项(例如姓名、学院、系、年份、国籍等)的ajax方法搜索数据。我有一个用于搜索的插入名称,其余字段比foreach循环中的字段为空,但此if(isset($\u GET[$field])&&!empty($\u GET['$field]])条件未成功并转到else循环

  $fields = array(
  'name' => TRUE,
  'gender' => TRUE,
  'colf' => TRUE,
  'deptf' => TRUE,
  'natf' => TRUE,
  'fstatusf' => TRUE,
  'fyearf' => TRUE
  );
 foreach ($fields as $field => $like) {
  if (isset($_GET[$field]) && !empty($_GET['$field'])) {
    $value = $_GET[$field];
    $search[] = $field . ( $like ? ('LIKE "%' . $value . '%"') : ('="' . $value . '"') );
       }
 } 
  if ($search) {
  $sql = 'SELECT * FROM fmaf WHERE ' . implode(' or ' . $search);
  }

else{
$sql="SELECT * FROM fmaf";

    }

您需要根据请求生成查询。 例如:

$sql = "select * from student where 1 = 1".(isset($name)?" AND name like '%$name%":"").(isset($country)?" AND country = '$country'":"").";";

您可以使用一种简单的方式,能够面对任何情况,如:

// define searchable fields, with option for LIKE|EQUAL (TRUE|FALSE)
$fields = [
  'name' => TRUE,
  'country' => TRUE,
  'address' => TRUE,
  'gender' => FALSE,
  'state' => FALSE
];
foreach ($fields as $field => $like) {
  if (isset($_GET[$field]) AND !empty($_GET['$field'])) {
    $value = $_GET[$field];
    // prepare WHERE condition item, depending on LIKE option
    $search[] = $field . (
      $like ? ('LIKE "%' . $value . '%"') : ('="' . $value . '"')
    );
  }
}
if ($search) {
  $sql = 'SELECT * FROM student WHERE ' . implode(' AND ' . $search);
}

最后我找到了解决办法,感谢cFreed和其他帮助我的人。我主要担心的是,如果用户只想搜索一个字段或多个字段,那么下面的答案对我很有帮助,也可能对某人有帮助:

if (empty($_GET['name']) && empty($_GET['gender']) && empty($_GET['colf']) && empty($_GET['deptf']) && empty($_GET['natf']) && empty($_GET['fstatusf']) && empty($_GET['fyearf']))
{
    $sql="select * from fmaf ";
}
else
{
 $wheres = array();

$sql = "select * from fmaf where ";

if (isset($_GET['name']) and !empty($_GET['name']))
{
    $wheres[] = "name like '%{$_GET['name']}%' ";
} 

if (isset($_GET['gender']) and !empty($_GET['gender']))
{
    $wheres[] = "gender = '{$_GET['gender']}'";
} 

if (isset($_GET['colf']) and !empty($_GET['colf']))
{
    $wheres[] = "college = '{$_GET['colf']}' ";
} 

if (isset($_GET['deptf']) and !empty($_GET['deptf']))
{
    $wheres[] = "department = '{$_GET['deptf']}' ";
} 

if (isset($_GET['natf']) and !empty($_GET['natf']))
{
    $wheres[] = "nationality = '{$_GET['natf']}' ";
} 

if (isset($_GET['fstatusf']) and !empty($_GET['fstatusf']))
{
    $wheres[] = "finalstatus = '{$_GET['fstatusf']}' ";
}

if (isset($_GET['fyearf']) and !empty($_GET['fyearf']))
{
    $wheres[] = "fyear = '{$_GET['fyearf']}' ";
} 

foreach ( $wheres as $where ) 
{
$sql .= $where . ' AND ';   //  you may want to make this an OR
  }
 $sql=rtrim($sql, "AND "); 

     }

只传递用户提供的输入字段请不要忘记验证/清理用户输入(例如,
$\u GET
)我尝试过,但出现错误,请参见上文我已更新了我的question@SalmanKarim. 我提出的代码(未经测试)中可能有一个bug,所以请准确地报告您得到的错误。我不明白:您更新的问题与原来的问题有很大不同。因此,首先,我建议的解决方案应该相应地进行更改。然后,不管怎样,你都不会解释你得到了哪个错误,也不会解释我的代码,也不会解释你的新问题……第一次加载所有数据时,但当我想用选择搜索数据时,不会出现任何数据。我没有收到任何错误:(.我认为if-else语句在逻辑上有一些问题。我有调试代码,值在ajax jquery中传递。显然,您当前的版本基于RiggsFolly答案,而不是我的:所以首先尝试修改我自己的建议。另一方面,在您当前的版本中,我注意到当字段搜索参数都为空时,您的查询显示
status
是一个不同的表,而第二个查询不考虑这个问题。