【问题标题】:How to get query to ignore empty search fields in my search engine如何让查询忽略我的搜索引擎中的空搜索字段
【发布时间】:2019-12-03 07:31:41
【问题描述】:

当我使用 mysqli 设置它时,我正在使用 and if 语句在搜索引擎中附加 $query .= "AND column LIKE $suchandsuch",但我正在将其更新为 PDO 并且正在努力弄清楚如何以确保它会忽略留空的字段。

表单字段为:条件、学位、专业、城市、州。条件是必需的,但没有其他要求,因此当它们为空时需要忽略这些字段。我觉得这应该是显而易见的,但我是 PDO 的新手并且很难过。

if(isset($_POST['submit'])){
  $condition = $_GET["condition"];

  if(isset($_GET["degree"]) && !empty($_GET["degree"])){
    $degree = $_GET["degree"];


 }
  if(isset($_GET["specialty"]) && !empty($_GET["specialty"])){
    $specialty = $_GET["specialty"];
  }
  if(isset($_GET["city"]) && !empty($_GET["city"])){
    $city = $_GET["city"];
  }
  if(isset($_GET["state"]) && !empty($_GET["state"])){
    $state = $_GET["state"];
  }

$query = $connection->prepare("SELECT
  `providers`.`provider_first_name`, `providers`.`provider_last_name`,
  `providers`.`p_date_added`, `providers`.`id`,
  (SELECT GROUP_CONCAT(DISTINCT `degree_type` SEPARATOR ', ')
          FROM `degrees`
          WHERE `providers`.`id` = `prov_id`
    ) AS all_degrees,
  (SELECT GROUP_CONCAT(DISTINCT `specialty` SEPARATOR ', ')
          FROM `specialties`
          WHERE `providers`.`id` = `prov_id`
    ) AS all_specialties,
  (SELECT   GROUP_CONCAT(DISTINCT CONCAT(`locations`.`city`, ', ',
    `locations`.`state`)
    SEPARATOR '<br>')
    FROM `locations`
    WHERE `providers`.`id` = `prov_id`
  ) AS all_locations,
  (SELECT GROUP_CONCAT(DISTINCT `practice_name` SEPARATOR ', ')
          FROM `practices`
          WHERE `providers`.`id` = `prov_id`
    ) AS all_practices,
  (SELECT GROUP_CONCAT(DISTINCT `link` SEPARATOR '<br>')
          FROM `links`
          WHERE `providers`.`id` = `prov_id`
    ) AS all_links,
  (SELECT GROUP_CONCAT(DISTINCT `condition_name` SEPARATOR ', ')
          FROM `conditions`
          WHERE `providers`.`id` = `prov_id`
    ) AS all_conditions,
  (SELECT AVG(`reviews`.`star_rating`)
          FROM `reviews`
          WHERE `providers`.`id` = `prov_id`
    ) AS rating
  FROM `providers`
  WHERE `all_conditions` LIKE :condition");


  if($degree && !empty($degree)){
      $query.= " AND (`all_degrees` LIKE :degree)";
      $degree = "%".$degree."%";
      $query->bindParam(':degree', $degree, PDO::PARAM_STR);
  }
  if($specialty && !empty($specialty)){
      $query.= " AND (`all_specialties` LIKE :specialty)";
      $specialty = "%".$specialty."%";
      $query->bindParam(':specialty', $specialty, PDO::PARAM_STR);
  }
  if($city && !empty($city)){
      $query .= " AND (`all_locations` LIKE :city)";
      $city = "%".$city."%";
      $query->bindParam(':city', $city, PDO::PARAM_STR);
  }
  if($state && !empty($state)){
      $query .= " AND (`all_locations` LIKE :state)";
      $state = "%".$state."%";
      $query->bindParam(':state', $state, PDO::PARAM_STR);
  }

$condition = "%".$condition."%";
$query->bindParam(':condition', $condition, PDO::PARAM_STR);

$query->execute();

【问题讨论】:

  • an* if 语句
  • 如果我将其附加到末尾而不是将其保留在 SELECT() 中,它仍然找不到该列:“找不到列:1054 'where 子句'中的未知列'all_conditions''跨度>

标签: php search dynamic replace field


【解决方案1】:

请添加不为空的条件。

if(isset($_GET["degree"]) && !empty($_GET["degree"])){
  $degree = $_GET["degree"];
}

【讨论】:

  • 这没有帮助:-(
【解决方案2】:

试试

var_dump($_GET["degree"]);

它们可能有一些价值

【讨论】:

    【解决方案3】:

    更新 - 不确定这是否是犹太洁食,但我设法通过以下代码获得了我想要的:

    include("config/db.php");
    include ("SearchEngineProvider.php");
    
    if(isset($_POST['submit'])){
      $condition = $_POST["condition"];
    
        if(isset($_POST["degree"])){
          $degree = $_POST["degree"];
        }
        if(isset($_POST["specialty"])){
          $spec = $_POST["specialty"];
        }
        if(isset($_POST["city"])){
          $city = $_POST["city"];
        }
        if(isset($_POST["state"])){
          $state = $_POST["state"];
        }
    
        if($condition != "")$whereCond = "AND condition_name LIKE :condition";
        if($degree != "")$whereDeg = "AND degree_type LIKE :degree";
        if($specialty != "")$whereSpec = "AND specialty LIKE :specialty";
        if($city != "")$whereCity = "AND city LIKE :city";
        if($state != "")$whereState = "AND state LIKE :state";
        $groupBy = " GROUP BY `id`";
    
    $query = $connection->prepare("SELECT `providers`.`id`,
      `providers`.`provider_first_name`, `providers`.`provider_last_name`,
      `providers`.`p_date_added`,
      `degrees`.`degree_type`,
      GROUP_CONCAT(DISTINCT `degrees`.`degree_type` SEPARATOR ', ') AS all_degress,
      `specialties`.`specialty`,
      GROUP_CONCAT(DISTINCT specialties.specialty SEPARATOR ', ') AS all_specialties,
      `locations`.`city`, `locations`.`state`,
      `practices`.`practice_name`,
      GROUP_CONCAT(DISTINCT practices.practice_name SEPARATOR ', ') AS all_practices,
      `links`.`link`,
      GROUP_CONCAT(DISTINCT links.link SEPARATOR ', ') AS all_links,
      `conditions`.`condition_name`,
      GROUP_CONCAT(DISTINCT conditions.condition_name SEPARATOR ', ') AS all_conditions
      FROM `providers`
      LEFT JOIN `degrees` ON `providers`.`id` = `degrees`.`prov_id`
      LEFT JOIN `specialties` ON `providers`.`id` = `specialties`.`prov_id`
      LEFT JOIN `locations` ON `providers`.`id` = `locations`.`prov_id`
      LEFT JOIN `practices` ON `providers`.`id` = `practices`.`prov_id`
      LEFT JOIN `links` ON `providers`.`id` = `links`.`prov_id`
      LEFT JOIN `conditions` ON `providers`.`id` = `conditions`.`prov_id`
      WHERE `id` IS NOT NULL {$whereCond} {$whereDeg} {$whereSpec} {$whereCity} {$whereState} {$groupBy}
      ");
    
        $condition = "%".$condition."%";
        $query->bindParam(':condition', $condition, PDO::PARAM_STR);
    
        if($whereDeg != ""){
          $degree = "%".$degree."%";
          $query->bindParam(':degree', $degree, PDO::PARAM_STR);
        }
        if($whereSpec != ""){
          $specialty = "%".$specialty."%";
          $query->bindParam(':specialty', $specialty, PDO::PARAM_STR);
        }
        if($whereCity != ""){
          $city = "%".$city."%";
          $query->bindParam(':city', $city, PDO::PARAM_STR);
        }
        if($whereState != ""){
          $state = "%".$state."%";
          $query->bindParam(':state', $state, PDO::PARAM_STR);
        }
    
    $query->execute();
    

    【讨论】:

      【解决方案4】:
      $city = @$_GET['city'];          // before PHP 7
      $city = $_GET['city'] ?? null;   // with PHP 7
      if (! empty($city)) {
          $query .= " AND (`all_locations` LIKE :city)";
          $query->bindParam(':city', "%$city%", PDO::PARAM_STR);
      }
      

      (等)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-04-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多