2015-01-02 137 views
0

我有一个搜索功能脚本,用于搜索多个输入字段。该脚本正在完美地搜索数据库。问题是当我使用OR运算符时,我必须输入两个字段进行搜索并且结果显示正常,但是如果我只输入1个字段,那么搜索不起作用并显示记录。如果我使用AND运算符,则必须输入1个字段,并显示相应的结果,但当输入2个字段时,结果为NULL。 AND & OR操作员给我的横向结果。多输入字段搜索问题

我希望当我输入1字段时,应该显示匹配结果,当我输入两个字段时,应该显示匹配两个字段的结果。

这是我的index.php搜索脚本。

//Perform Search from our database 
if(isset($_POST['action_type'])) 
{ 
    echo ''; 
    if ($_POST['action_type'] == 'search') 
    { 
     $search = mysqli_real_escape_string($link, strip_tags($_POST['searchText'])); 
     $search_add = mysqli_real_escape_string($link, strip_tags($_POST['searchAddress'])); 

     $sql = "select contact_id, CONCAT(first_name,' ' , last_name) as contact_name, 
       contact_no, residential_address, 
       company, company_address from tblcontact 
       where CONCAT(first_name, ' ', Last_name) like '%$search%' or contact_no like '%$search_add%' order by contact_id asc"; 


     $result = mysqli_query($link, $sql); 

     if(!$result) 
     { 
      echo mysqli_error($link); 
      exit(); 
     } 




     //Loop through each row on array and store the data to $contact_list[] 
     while($rows = mysqli_fetch_array($result)) 
     { 
      $contact_list[] = array('contact_id' => $rows['contact_id'], 
            'contact_name' => $rows['contact_name'], 
            'contact_no' => $rows['contact_no'], 
            'residential_address' => $rows['residential_address'], 
            'company' => $rows['company'], 
            'company_address' => $rows['company_address']); 
     } 
     include 'contactlist.php'; 
     exit(); 
    } 
} 

这是我的contactlist.php形式。

<center><div style="margin-bottom: 5px;"> 
      <form method="POST" action="index.php" > 
       <table> 
       <tr> 
       <th>Name</th> 
       <td><input type="text" id="searchText" name="searchText" style="width: 300px; margin-left: 14px;"/></td> 
       </tr> 
       <tr> 
       <th>Contact</th> 
       <td><input type="text" id="searchAddress" name="searchAddress" style="width: 300px; margin-left: 14px;"/></td> 
       </tr> 
       <input type="hidden" name="action_type" value="search"/> 
       </table><br> 
       &nbsp;&nbsp;&nbsp;<input type="submit" value="Refresh Search" onClick="window.location.href='index.php'"> 


      </form> 

      </div> 
      <div style="max-height: 350px; overflow:auto;"> 
      <table class="pbtable" border="1"> 
       <thead> 
        <tr> 
         <th> 
          ID 
         </th> 
         <th> 
          Name 
         </th> 
         <th> 
          Contact # 
         </th> 
         <th> 
          Res. Address 
         </th> 
         <th> 
          Company 
         </th> 
         <th> 
          Company Address 
         </th> 
         <th></th><th></th> 
        </tr> 
       </thead><br><br><br> 
       <tbody> 
        <?php foreach($contact_list as $contact) : ?> 
         <tr> 
          <td> 
           <?php echo $contact["contact_id"]; ?> 
          </td> 
          <td> 
           <?php echo $contact["contact_name"]; ?> 
          </td> 
          <td> 
           <?php echo $contact["contact_no"]; ?> 
          </td> 
          <td> 
           <?php echo $contact["residential_address"]; ?> 
          </td> 
          <td> 
           <?php echo $contact["company"]; ?> 
          </td> 
          <td> 
           <?php echo $contact["company_address"]; ?> 
          </td> 
          <td> 
           <form method="post" action="index.php"> 
            <input type="hidden" name="ci" 
            value="<?php echo $contact["contact_id"]; ?>" /> 
            <input type="hidden" name="action" value="edit" /> 
            <input type="submit" value="Edit" /> 
           </form> 
          </td> 
          <td> 
           <form method="POST" action="index.php" 
           onSubmit="return ConfirmDelete();"> 
            <input type="hidden" name="ci" 
            value="<?php echo $contact["contact_id"]; ?>" /> 
            <input type="hidden" name="action" value="delete" /> 
            <input type="submit" value="Delete" /> 
           </form> 
          </td> 
         <tr> 
        <?php endforeach; ?> 
       </tbody> 
      </table> 
      </div> 

如果输入1个字段并输入2个字段,则搜索应起作用。这个问题的解决方案是什么?

+0

即使我最近遇到同样的问题..我用'if'条件来克服它..我建议你也使用条件.. 'if($ variable!=“”){//执行一个查询} elseif($ variable2!==“”){//执行另一个查询}' –

+0

使用我的脚本显示它请 – Kanishk

回答

0

您需要检查是否字段为空:

if(!empty($_POST['searchText']) 
    $search = mysqli_real_escape_string($link, strip_tags($_POST['searchText'])); 
if(!empty($_POST['searchAddress']) 
    $search_add = mysqli_real_escape_string($link, strip_tags($_POST['searchAddress'])); 

然后,根据这个你应该更改查询。

if(!empty($_POST['searchText'] && !empty($_POST['searchAddress']) 
{ 
    $sql = "select contact_id, CONCAT(first_name,' ' , last_name) as contact_name, 
       contact_no, residential_address, 
       company, company_address from tblcontact 
       where CONCAT(first_name, ' ', Last_name) like '%$search%' or contact_no like '%$search_add%' order by contact_id asc"; 
} 
else if(empty($_POST['searchText'] && !empty($_POST['searchAddress']) 
{ 
    $sql = "select contact_id, CONCAT(first_name,' ' , last_name) as contact_name, 
       contact_no, residential_address, 
       company, company_address from tblcontact 
       where contact_no like '%$search_add%' order by contact_id asc"; 
} 
else if(!empty($_POST['searchText'] && empty($_POST['searchAddress']) 
{ 
    $sql = "select contact_id, CONCAT(first_name,' ' , last_name) as contact_name, 
       contact_no, residential_address, 
       company, company_address from tblcontact 
       where CONCAT(first_name, ' ', Last_name) like '%$search%' order by contact_id asc"; 
} 

根据你想要达到的AND情况也调整你的查询。

+0

工作完美。谢谢。 – Kanishk

+0

不客气。 – Aris

0

请试试这个版本的SQL查询:

$sql = "select contact_id, CONCAT(first_name,' ' , last_name) as contact_name, 
       contact_no, residential_address, 
       company, company_address from tblcontact 
       where CONCAT(first_name, ' ', Last_name) like '%" 
       .(empty($search) ? "" : ($search."%")) 
       ."' AND contact_no like '%" 
       .(empty($search_add) ? "" : ($search_add."%")) 
       ."' order by contact_id asc"; 
+0

这给出一个NULL值,如果两个字段都输入搜索 – Kanishk

+0

Thx for reply。是否有可能你的表'first_name,Last_name,contact_no'中的任何字段可能具有NULL值? –

+0

是的..虽然搜索他们可以有一个空值.. – Kanishk