简体   繁体   English

elseif搜索查询不起作用

[英]elseif search query not working

I have a search form that has 3 elements keywords, Industry ( Dropdown ) & location 我有一个包含3个元素的关键字的搜索表单:行业(下拉列表)和位置

I am trying to create search function that will query the DB if all 3 elements are selected or based on individual values OR a combination of any elements having $_POST data submitted 我正在尝试创建搜索功能,该功能将查询数据库是否选择了所有3个元素,或者是基于单个值还是已提交$ _POST数据的任何元素的组合

Its just not working 它只是不起作用

My code for the queries is below 我的查询代码如下

        if(isset($_POST['keywords']))
    {
    $keywords = $_POST['keywords'];
    $viewq = mysqli_query($con,"SELECT * FROM `listings` WHERE (`title` LIKE '%$keywords%' OR `description` LIKE '%$keywords%') LIMIT 0,50");
    $resultsfor = $_POST['keywords'];
    }
    elseif(isset($_POST['keywords']) && isset($_POST['location']))
    {
    $keywords = $_POST['keywords'];
    $location = $_POST['location'];
    $viewq = mysqli_query($con,"SELECT * FROM `listings` WHERE (`title` LIKE '%$keywords%' OR `description` LIKE '%$keywords%') AND (`location` LIKE '%$location%') LIMIT 0,50");
    $resultsfor = $keywords .' jobs in '.$location;
    }
    elseif(isset($_POST['keywords']) && isset($_POST['location']) && !empty($_POST['industry']))
    {
    $keywords = $_POST['keywords'];
    $location = $_POST['location'];
    $catno = $_POST['industry'];
    $viewq = mysqli_query($con,"SELECT * FROM `listings` WHERE (`title` LIKE '%$keywords%' OR `description` LIKE '%$keywords%') AND (`location` LIKE '%$location%') AND (`catno` = '$catno') LIMIT 0,50");
    $resultsfor = $keywords .' jobs in '.$location;
    }
    elseif(isset($_POST['industry']) && empty($_POST['location']))
    {
    $industry = $_POST['industry'];
    $viewq = mysqli_query($con,"SELECT * FROM `listings` WHERE `catno` = '$industry' LIMIT 0,50");
    $resultsfor = $_POST['industry']. ' jobs';
    }
    elseif(isset($_POST['industry'])&& isset($_POST['location']))
    {
    $industry = $_POST['industry'];
    $location = $_POST['location'];
    $viewq = mysqli_query($con,"SELECT * FROM `listings` WHERE (`catno` = '$industry') AND (`location` LIKE '%$location%') LIMIT 0,50");
    $resultsfor = $_POST['industry']. ' jobs in '.$location;
    }
    elseif(isset($_POST['location']))
    {
    $location = $_POST['location'];
    $viewq = mysqli_query($con,"SELECT * FROM `listings` WHERE (`location` LIKE '%$location%') LIMIT 0,50");
    $resultsfor = $_POST['location']. ' jobs';
    }

    $view = mysqli_fetch_assoc($viewq);

Would really appreciate some help, Ive spent loads of time on this Thanks in advance 非常感谢您的帮助,为此我花了很多时间在此先感谢

** It does pull data although not correct, If i search for warehouse ( leave industry dropdown blank ) but set location to Leicester it still queries ALL results not Leicester If I select an Industry from the dropdown menu and leave other fields blank, I get all jobs in all categories not just the category selected **它确实提取了数据,尽管不正确,如果我搜索仓库(将行业下拉列表留为空白),但是将位置设置为莱斯特,它仍然会查询所有结果,而不是莱斯特。如果我从下拉菜单中选择一个行业并将其他字段保留为空白,则会得到所有类别中的所有作业,而不仅仅是选定的类别

Getting myself confused now 现在让自己感到困惑

I imagine your issues are due to the complicated way you have structured your if-else conditions. 我想您的问题是由于您构建if-else条件的复杂方式造成的。 A much simpler way would be to do it as follows... 一种更简单的方法如下

// Get the input variables
$keywords = (isset($_POST['keywords']) && strlen($_POST['keywords']) > 0) ? $_POST['keywords'] : null;
$location = (isset($_POST['location']) && strlen($_POST['location']) > 0) ? $_POST['location'] : null;
$catno = (isset($_POST['industry']) && strlen($_POST['industry']) > 0) ? $_POST['industry'] : null;

$whereUsed = false;
$whereString = "";
$resultsfor = "";

// Add to the WHERE clause if keywords exists.
if ($keywords !== null) {
    if (! $whereUsed) {
        $whereString .= 'WHERE ';
        $whereUsed = true;
    } else {
        $whereString .= ' AND ';
    }
    $whereString .= "(title LIKE '%{$keywords}%' OR description LIKE '%{$description}%')";
    if ($catno === null) {
        $resultsfor .= $keywords;
    }
}
// Add to the WHERE clause if catno exists.
if ($catno !== null) {
    if (! $whereUsed) {
        $whereString .= 'WHERE ';
        $whereUsed = true;
    } else {
        $whereString .= ' AND ';
    }
    $whereString .= "(catno = '{$catno}')";
    $resultsfor .= $catno;
}
// Add to the WHERE clause if location exists.
if ($location !== null) {
    if (! $whereUsed) {
        $whereString .= 'WHERE ';
        $whereUsed = true;
    } else {
        $whereString .= ' AND ';
    }
    $whereString .= "(location LIKE '%{$location}%')";
    if ($catno === null && $keywords === null) {
        $resultsfor = "{$location} jobs";
    } else {
        $resultsfor .= " jobs in {$location}";
    }
}

// Build the SQL query using the WHERE clauses we've built up
$sqlQuery = "SELECT * FROM listings {$whereString} LIMIT 0, 50";

// Execute the query and fetch the response
$viewq = mysqli_query($con, $sqlQuery);
$view = mysqli_fetch_assoc($viewq);

There is no need for the if-else-if format you have in your original code. 原始代码中不需要if-else-if格式。 You are simply adding WHERE clauses based on whether or not a variable is set... so you should do exactly that. 您只是根据是否设置了变量添加了WHERE子句...因此您应该完全这样做。

Please note that in your code, and in my example the SQL queries are vulnerable to SQL injection. 请注意,在您的代码(在我的示例中)中,SQL查询容易受到SQL注入的攻击。 I strongly suggest looking into prepared statements . 我强烈建议研究准备好的陈述

I see some erros in your if statements. 我在您的if语句中看到了一些错误。 You should test from most specifc to low restricted. 您应该从最具体到最低限制进行测试。 Your first elseif for example will never happen, because in this case the first if always occours. 例如,您的第一个elseif将永远不会发生,因为在这种情况下,第一个( if总是)会发生。

There should be nine combinations to filter your query. 应该有九种组合来过滤您的查询。 Your conditions should be 您的条件应该是

if(isset($_POST['keywords']) && !isset($_POST['location']) && !isset($_POST['industry'])){}
elseif(isset($_POST['keywords']) && !isset($_POST['location']) && isset($_POST['industry'])){}
elseif(isset($_POST['keywords']) && isset($_POST['location']) && !isset($_POST['industry'])){}
elseif(isset($_POST['keywords']) && isset($_POST['location']) && isset($_POST['industry'])){}
elseif(!isset($_POST['keywords']) && !isset($_POST['location']) && isset($_POST['industry'])){}
elseif(!isset($_POST['keywords']) && isset($_POST['location']) && !isset($_POST['industry'])){}
elseif(!isset($_POST['keywords']) && isset($_POST['location']) && isset($_POST['industry'])){}
  if (isset($_POST['keywords']) && isset($_POST['location']) && 
     isset($_POST['industry'])) {
     $keywords = $_POST['keywords'];
     $location = $_POST['location'];
     $catno = $_POST['industry'];
     $viewq = mysqli_query($con, "SELECT * FROM `listings` WHERE (`title` LIKE '%$keywords%' OR `description` LIKE '%$keywords%') AND (`location` LIKE '%$location%') AND (`catno` = '$catno') LIMIT 0,50");
     $resultsfor = $keywords . ' jobs in ' . $location;
  } elseif (isset($_POST['keywords']) && isset($_POST['location']) && 
    empty($_POST['industry'])) {
           $keywords = $_POST['keywords'];
      $location = $_POST['location'];
      $viewq = mysqli_query($con, "SELECT * FROM `listings` WHERE (`title` 
     LIKE '%$keywords%' OR `description` LIKE '%$keywords%') AND (`location` 
      LIKE '%$location%') LIMIT 0,50");
    $resultsfor = $keywords . ' jobs in ' . $location;
    } elseif (isset($_POST['keywords']) && empty($_POST['location']) && 
     empty($_POST['industry'])) {
      $keywords = $_POST['keywords'];
      $viewq = mysqli_query($con, "SELECT * FROM `listings` WHERE (`title` 
      LIKE '%$keywords%' OR `description` LIKE '%$keywords%') LIMIT 0,50");
      $resultsfor = $_POST['keywords'];
   } elseif (empty($_POST['keywords']) && isset($_POST['industry']) && 
   empty($_POST['location'])) {
     $industry = $_POST['industry'];
     $viewq = mysqli_query($con, "SELECT * FROM `listings` WHERE `catno` = 
    '$industry' LIMIT 0,50");
     $resultsfor = $_POST['industry'] . ' jobs';
  } elseif (empty($_POST['keywords']) && isset($_POST['industry']) && 
     isset($_POST['location'])) {
     $industry = $_POST['industry'];
       $location = $_POST['location'];
     $viewq = mysqli_query($con, "SELECT * FROM `listings` WHERE (`catno` = 
     '$industry') AND (`location` LIKE '%$location%') LIMIT 0,50");
    $resultsfor = $_POST['industry'] . ' jobs in ' . $location;
} elseif (empty($_POST['keywords']) && empty($_POST['industry']) && 
isset($_POST['location'])) {
       $location = $_POST['location'];
           $viewq = mysqli_query($con, "SELECT * FROM `listings` WHERE 
     (`location` LIKE '%$location%') LIMIT 0,50");
      $resultsfor = $_POST['location'] . ' jobs';
   }

     $view = mysqli_fetch_assoc($viewq);

please try this 请尝试这个

I tried to minimize your code and check this and tell if it works correct or not 我试图最小化您的代码并进行检查,并判断其是否正确

<?php

    //Enter your code here, enjoy!
    $query          = "SELECT * FROM listings ";
    $eQuery         = "";
    $resultsfor     = "";

    if(isset($_POST['keywords']) && !empty($_POST['keywords']){
        $eQuery     .= " (title LIKE '%".addslashes($_POST['keywords'])."%' OR description LIKE '%".addslashes($_POST['keywords'])."%') ";
        $resultsfor .= $_POST['keywords'];
    }

    if(isset($_POST['location']) && !empty($_POST['location']){
        $eQuery     .=  " (`location` LIKE '%".addslashes($_POST['location'])."%') ";
        $resultsfor .= ' jobs in '.$location;
    }

    if(isset($_POST['industry']) && !empty($_POST['industry'])){
        $eQuery     .= " (`catno` = '".$_POST['industry']."')";
        $resultsfor = (isset($_POST['keywords']) && (isset($_POST['location'])))? $_POST['keywords'] .' jobs in '.$_POST['location'] ;
    }

    $query = mysql_query($con, $query.$eQuery." LIMIT 0,50");

    $view = mysqli_fetch_assoc($query);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM