mysql从多个文本框中搜索多个单词

时间:2018-03-28 10:56:58

标签: php mysql

我已经形成一个包含三个文本框并且用户填写它并点击搜索按钮它将重定向到另一个页面并显示结果..一切都很好..但问题是用户在同一文本框中输入两个单词

(例如:铁路殖民地)

它不会显示详细信息..为此,我使用expolde方法为多字搜索插入“+”符号

这是代码

 <?php  
     if(isset($_GET["search"]))  
     {  
          $condition = '';  
          $query = explode(" ", $_GET["search"]);  
          foreach($query as $text)  
          {  
               $condition .= "address LIKE '%".mysqli_real_escape_string($connect, $text)."%' OR   " ;  
          }  
          $condition = substr($condition, 0, -4);  
          $sql_query = "SELECT * FROM jposts WHERE " . $condition;  

          $result = mysqli_query($connect, $sql_query);

          if(mysqli_num_rows($result) > 0)  
          {  
               while($row = mysqli_fetch_array($result))  
               {  
                    echo '<tr><td>'.$row["address"].'</td></tr>';  
               }  
          }  
          else  
          {  
               echo '<label>Data not Found</label>';  
          }  
     }  
     ?> 

它适用于一个带有多字搜索的文本框..但是这三个文本框呢

我以前的搜索查询是

$stmt = $search->runQuery("SELECT * FROM jposts WHERE city LIKE '%".$city."%' 
   AND address LIKE '%".$location."%' AND business_name LIKE 
  '%".$category."%'");

我想为我拥有的所有三个文本框实现多字搜索

for ex: In textbox1 user enters  : tirupati (city)
        In textbox2 user enters  : railway colony (location)
        In textbox3 user enters  : Lic agent (catagory)

点击搜索后,我想根据以上数据显示相关信息

1 个答案:

答案 0 :(得分:0)

使用urldecode我们可以从get params中删除“+”。

查看提到的代码:

if ($_GET) {

  $cityQstr = explode(" ", urldecode($_GET['city']));
  $locationQstr = explode(" ", urldecode($_GET['location']));
  $catagoryQstr = explode(" ", urldecode($_GET['catagory']));

  $condition = [
      $condition . "city LIKE '%" . implode("%' OR city LIKE '%", $cityQstr) . "%'",
      $condition . "address LIKE '%" . implode("%' OR address LIKE '%", $locationQstr) . "%'",
      $condition . "business_name LIKE '%" . implode("%' OR business_name LIKE '%", $catagoryQstr) . "%'"
  ];


  $stmt =  "SELECT * FROM jposts WHERE " . implode(" AND ", $condition);

}