2017-09-17 126 views
-1

我有部分世界搜索問題。MySQL PHP部分搜索

下面的代碼工作,但只適用於全文搜索。如果我只在搜索框中輸入部分單詞,則不起作用。請你能給我一個提示,以便能夠進行部分單詞搜索?

<?php include "connection.php"; 


$sql = "SELECT * FROM Klienci "; 

if (isset($_POST['search'])) { 

    $search_term = mysql_real_escape_string($_POST['search_box']); 

    $sql .= "WHERE nazwaKlienta = '{$search_term}' "; 
    $sql .= " OR adresMail = '{$search_term}' "; 
    $sql .= " OR adresMail2 = '{$search_term}' "; 
} 

$query = mysql_query($sql) or die(mysql_error()); 

?> 


<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <meta charset="utf-8"> 
    <meta http-equiv="X-UA-Compatible" content="IE=edge"> 
    <meta name="viewport" content="width=device-width, initial-scale=1"> 
    <title>BAZA | Dashboard</title> 
    <!-- Bootstrap core CSS --> 
    <link href="css/bootstrap.min.css" rel="stylesheet"> 

    <link href="css/jumbotron-narrow.css" rel="stylesheet"> 
</head> 
<body> 

    <div class="container"> 
    <div class="header"> 
     <ul class="nav nav-pills pull-right"> 
     <li class="active"><a href="index.php">Home</a></li> 
     <li><a href="add_customer.php">Dodaj Firmę</a></li> 
     </ul> 
     <h3 class="text-muted">ITalents</h3> 
    </div> 

    <div class="row marketing"> 
     <div class="col-lg-12"> 

     <h2>KLIENCI</h2> 
     <table class="table table-striped"> 

      <form name="search_form" method="POST" action="display_data.php"> 
      Search: <input type="text" name="search_box" value ="" /> 
      <input type="submit" name="search" value="Search the table..." > 
      </form> 

      <tr> 
      <th>id</th> 
      <th>Klient</th> 
      <th>Email</th> 
      <th>Email 2</th> 
      <th>Data dodania do bazy</th> 
      </tr> 

      <?php while ($row = mysql_fetch_array($query)) { 

      //Display customer info 
      $output ='<tr>'; 
      $output .='<td>'.$row['id'].'</td>'; 
      $output .='<td>'.$row['nazwaKlienta'].'</td>'; 
      $output .='<td>'.$row['adresMail'].'</td>'; 
      $output .='<td>'.$row['adresMail2'].'</td>'; 
      $output .='<td>'.$row['dataDodania'].'</td>'; 
      $output .='<td><a href="edit_customer.php?id='.$row['id'].'" class="btn btn-default btn-sm">Edit</a></td>'; 
      $output .='<td><a href="delete_customer.php?id='.$row['id'].'" class="btn btn-default btn-sm">Delete</a></td>'; 
      $output .='</tr>'; 

      //Echo output 
      echo $output; 
      ?> 
      <?php } ?> 

      </table> 

     </div> 
     </div> 

    </div> 

    <div class="footer"> 
     <p>&copy; Company 2014</p> 
    </div> 

    </div> <!-- /container --> 


</body> 
</html> 
+2

您需要使用LIKE或正則表達式。 –

+1

使用'like'和通配符,或查看全文搜索。也不要使用'mysql_ *'再使用'PDO'或'MySQLi'。 https://dev.mysql.com/doc/refman/5.7/en/fulltext-search.html – chris85

+0

如果您在問這裏之前先用盡在線搜索,那麼對您和SO社區都有好處。如果你只是搜索谷歌的這篇文章的標題,你會發現大量的資源來幫助你解決它...說,LIKE關鍵字將是你要找的。 –

回答

0

下面的代碼應該可以工作。你必須使用LIKE而不是使用平等。例如:WHERE something like'%something2%'

<?php include "connection.php"; 


$sql = "SELECT * FROM Klienci "; 

if (isset($_POST['search'])) { 

    $search_term = mysql_real_escape_string($_POST['search_box']); 

    $sql .= "WHERE nazwaKlienta LIKE '%{$search_term}%' "; 
    $sql .= " OR adresMail LIKE '%{$search_term}%' "; 
    $sql .= " OR adresMail2 LIKE '%{$search_term}%' "; 
} 

$query = mysql_query($sql) or die(mysql_error()); 

?> 


<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <meta charset="utf-8"> 
    <meta http-equiv="X-UA-Compatible" content="IE=edge"> 
    <meta name="viewport" content="width=device-width, initial-scale=1"> 
    <title>BAZA | Dashboard</title> 
    <!-- Bootstrap core CSS --> 
    <link href="css/bootstrap.min.css" rel="stylesheet"> 

    <link href="css/jumbotron-narrow.css" rel="stylesheet"> 
</head> 
<body> 

    <div class="container"> 
    <div class="header"> 
     <ul class="nav nav-pills pull-right"> 
     <li class="active"><a href="index.php">Home</a></li> 
     <li><a href="add_customer.php">Dodaj Firmę</a></li> 
     </ul> 
     <h3 class="text-muted">ITalents</h3> 
    </div> 

    <div class="row marketing"> 
     <div class="col-lg-12"> 

     <h2>KLIENCI</h2> 
     <table class="table table-striped"> 

      <form name="search_form" method="POST" action="display_data.php"> 
      Search: <input type="text" name="search_box" value ="" /> 
      <input type="submit" name="search" value="Search the table..." > 
      </form> 

      <tr> 
      <th>id</th> 
      <th>Klient</th> 
      <th>Email</th> 
      <th>Email 2</th> 
      <th>Data dodania do bazy</th> 
      </tr> 

      <?php while ($row = mysql_fetch_array($query)) { 

      //Display customer info 
      $output ='<tr>'; 
      $output .='<td>'.$row['id'].'</td>'; 
      $output .='<td>'.$row['nazwaKlienta'].'</td>'; 
      $output .='<td>'.$row['adresMail'].'</td>'; 
      $output .='<td>'.$row['adresMail2'].'</td>'; 
      $output .='<td>'.$row['dataDodania'].'</td>'; 
      $output .='<td><a href="edit_customer.php?id='.$row['id'].'" class="btn btn-default btn-sm">Edit</a></td>'; 
      $output .='<td><a href="delete_customer.php?id='.$row['id'].'" class="btn btn-default btn-sm">Delete</a></td>'; 
      $output .='</tr>'; 

      //Echo output 
      echo $output; 
      ?> 
      <?php } ?> 

      </table> 

     </div> 
     </div> 

    </div> 

    <div class="footer"> 
     <p>&copy; Company 2014</p> 
    </div> 

    </div> <!-- /container --> 


</body> 
</html> 
+0

您最歡迎:) –

+3

無論如何,您應該避免使用mysql,我建議您學習mysqli或PDO。如果您在較新版本的PHP上運行此代碼,它將無法工作,並且也會使您的站點容易受到SQL注入的攻擊。學習mysqli就像mysql一樣簡單。 –

+2

對@SMJobayerAlam的更新,請注意,mysql作爲數據庫可以使用。 PHP'mysql_ *'函數不應該再被使用了。 – chris85