【问题标题】:PHP search with multiple fields具有多个字段的 PHP 搜索
【发布时间】:2015-12-15 19:27:57
【问题描述】:

我想使用带有多个字段搜索选项(例如姓名、学院、部门、年份、国籍等)的 ajax 方法搜索数据。我有用于搜索的插入名称,其余字段为空,而不是进入 foreach 循环,但是这个 if (isset($_GET[$field]) && !empty($_GET['$field'])) 条件不成功并且去了否则循环

  $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";

    }

【问题讨论】:

  • 只传递用户提供的那些输入字段
  • 拜托拜托,不要忘记验证/清理用户输入(例如$_GET

标签: php mysql search


【解决方案1】:

最后我找到了解决方案,感谢 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 "); 

     }

【讨论】:

    【解决方案2】:

    您需要根据请求构建查询。 一个玩具示例是这样的:

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

    【讨论】:

      【解决方案3】:

      你可以用一个简单的方法,可以面对任何情况,像这样:

      // 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);
      }
      

      【讨论】:

      • 我试过了,但是出错了,请看上面我已经更新了我的问题
      • @SalmanKarim。我提出的代码中可能存在错误(未经测试),因此请准确地报告您遇到的错误。
      • 我不明白:您更新的问题与原来的问题完全不同。所以首先我提出的解决方案应该相应地改变。那么无论如何你没有解释你得到哪个错误,也没有我的代码,也没有你的新问题......
      • 第一次加载了所有数据,但是当我想通过选择搜索数据时,没有数据出现。我没有收到任何错误:(。我认为 if else 语句存在一些逻辑问题。我有调试代码并且值在 ajax jquery 中传递。
      • 显然您当前的版本是基于 RiggsFolly 的回答,而不是我的:所以首先尝试调整我自己的建议。另一方面,在您当前的版本中,我注意到当字段搜索参数全部为空时,您的查询显示 status 是一个不同的表,而第二个查询没有处理这个问题。
      猜你喜欢
      • 1970-01-01
      • 2016-11-07
      • 1970-01-01
      • 1970-01-01
      • 2017-10-02
      • 2020-12-08
      • 1970-01-01
      • 2017-11-06
      • 1970-01-01
      相关资源
      最近更新 更多