【问题标题】:Search function not working for multiple words搜索功能不适用于多个单词
【发布时间】:2015-12-27 21:39:56
【问题描述】:

我一直在一个网站上使用这个搜索功能,它从发布的单词中提取单词并搜索数据库。 Iv 对其进行了调整,以便它可以接受来自网站另一部分的内容,这些内容通过 GET 发送给它。我遇到的问题是,当您向它发送多个单词时,例如 searchstock=some search words 它不起作用。如果我只发送单个单词,例如使用 GET 的 searchstock=word,它工作得很好,它只有在发送多个单词时它不起作用,只会在数据库中显示所有结果。奇怪的是多词搜索可以通过 POST 正常工作。

    // Begin Search Section >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    function search($db) {
        if (isset($_POST['searchstock'])){$words = $_POST['searchstock'];}
        if (isset($_GET['searchstock'])){$words = $_GET['searchstock'];}

    $searchQuery = ''; // search query is empty by default
    $searchCondition = "(cultivar LIKE '%%' OR description LIKE '%%' OR species LIKE '%%' OR colour LIKE '%%')";
    $searchFieldName = 'cultivar'; // name of the field to be searched
    $searchFieldName2 = 'description';
    $searchFieldName3 = 'species';
    $searchFieldName4 = 'colour';
    if(isset($_POST['searchstock']))
    { // check if a query was submitted
    $searchQuery = trim($_POST['searchstock']); // getting rid of unnecessary white space
    $searchTerms = explode(" ", $searchQuery); // Split the words
    $searchCondition = "($searchFieldName LIKE '%" . implode("%' OR $searchFieldName LIKE '%", $searchTerms) . "%')"; // Forming the condition for the sql
    $searchCondition .= " OR ($searchFieldName2 LIKE '%" . implode("%' OR $searchFieldName2 LIKE '%", $searchTerms) . "%')";
    $searchCondition .= " OR ($searchFieldName3 LIKE '%" . implode("%' OR $searchFieldName3 LIKE '%", $searchTerms) . "%')";
    $searchCondition .= " OR ($searchFieldName4 LIKE '%" . implode("%' OR $searchFieldName4 LIKE '%", $searchTerms) . "%')";
    }
    else if(isset($_GET['searchstock']))
    { // check if a query was submitted
    $searchQuery = trim($_GET['searchstock']); // getting rid of unnecessary white space
    $searchTerms = explode(" ", $searchQuery); // Split the words
    $searchCondition = "($searchFieldName LIKE '%" . implode("%' OR $searchFieldName LIKE '%", $searchTerms) . "%')"; // Forming the condition for the sql
    $searchCondition .= " OR ($searchFieldName2 LIKE '%" . implode("%' OR $searchFieldName2 LIKE '%", $searchTerms) . "%')";
    $searchCondition .= " OR ($searchFieldName3 LIKE '%" . implode("%' OR $searchFieldName3 LIKE '%", $searchTerms) . "%')";
    $searchCondition .= " OR ($searchFieldName4 LIKE '%" . implode("%' OR $searchFieldName4 LIKE '%", $searchTerms) . "%')";
    }
    // the rest is just database connection and retrieving the results
    $sql = <<<SQL
    SELECT * FROM stock WHERE $searchCondition;
    SQL;
    if(!$result = $db->query($sql)){ die('There was an error running the query [' . $db->error . ']');}
    while($row = $result->fetch_assoc()){        //Show your results here eg: $row['field1']
    $searchid = $row['id']; 
    $searchgenusid = $row['genusid'];
    // End Search
            // Search DB Function Start //
    $sql4 = <<<SQL
    SELECT * FROM `stock` WHERE `id` = '$searchid';
    SQL;
    if(!$stockresult = $db->query($sql4)){ die('There was an error running the query [' . $db->error . ']');}
    while($stockrow = $stockresult->fetch_assoc()){ 

【问题讨论】:

  • 不确定,但在 get 方法中只允许使用 ascii 字符。也许这就是原因
  • 我记得空间不是 ascii 字符..

标签: php mysql search post get


【解决方案1】:
function search($db) {
    $words=''
    if (isset($_POST['searchstock'])){$words = $_POST['searchstock'];}
    if (isset($_GET['searchstock'])){$words = $_GET['searchstock'];}
    if (!empty($words){
      $searchQuery = ''; // search query is empty by default
      $searchCondition = "(cultivar LIKE '%%' OR description LIKE '%%' OR species LIKE '%%' OR colour LIKE '%%')";
      $searchFieldName = 'cultivar'; // name of the field to be searched
      $searchFieldName2 = 'description';
      $searchFieldName3 = 'species';
      $searchFieldName4 = 'colour';
      $searchQuery = trim(words); // getting rid of unnecessary white space
      $searchTerms = explode(" ", $searchQuery); // Split the words
      $searchCondition = "($searchFieldName LIKE '%" . implode("%' OR $searchFieldName LIKE '%", $searchTerms) . "%')"; // Forming the condition for the sql
      $searchCondition .= " OR ($searchFieldName2 LIKE '%" . implode("%' OR $searchFieldName2 LIKE '%", $searchTerms) . "%')";
      $searchCondition .= " OR ($searchFieldName3 LIKE '%" . implode("%' OR $searchFieldName3 LIKE '%", $searchTerms) . "%')";
      $searchCondition .= " OR ($searchFieldName4 LIKE '%" . implode("%' OR $searchFieldName4 LIKE '%", $searchTerms) . "%')";
      // the rest is just database connection and retrieving the results
      $sql = <<<SQL
      SELECT * FROM stock WHERE $searchCondition;
      SQL;
      if(!$result = $db->query($sql)){ 
        die('There was an error running the query [' . $db->error . ']');
      }
      while($row = $result->fetch_assoc()){
        $searchid = $row['id']; 
        $searchgenusid = $row['genusid'];

        //if its work on _POST then should also work on _GET
        //do what ever you want
      }
    }
}

【讨论】:

  • 我在复制和粘贴时遇到错误,我可以看到后面的行不是用 ; 关闭的。 , 如果空需要用 ) 关闭,但不确定它是在哪里引起的。
  • 我没有检查我只是为了更好地理解你,但如果你想使用单个单词的多列使用 SELECT * FROM table WHERE CONCATENATE_WS(',', col1, col2, col3) LIKE %keyword%。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多