2017-05-29 95 views
0

我堅持了幾天的這個問題。 我想開發該系統的邏輯是在這裏使用php和mysql的表單上的多個搜索字段

enter image description here

這是圖像的代碼

<?php 
session_start(); 
?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
    <title>Search</title> 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
    <!-- <link rel="stylesheet" type="text/css" href="style.css"/> --> 
</head> 
<body> 
    <form action="process/searchprocess.php" method="GET"> 
     <table width="100%"> 
      <tr> 
       <th> 
        Client Ic<br><input type="text" name="client_name" /> 
       </th> 
       <th> 
        Client Ic<br><input type="text" name="client_ic" /> 
       </th> 
       <th> 
        Client Address <br><input type="text" name="client_add" /> 
       </th> 
      </tr> 
     </table> 
     <table width="100%"> 
      <tr> 
       <th> 
        <br><input type="submit" value="Search" align="center" /> 
       </th> 
      </tr> 
     </table> 
    </form> 
</body> 
</html> 

這是我的過程代碼

<?php 
    session_start(); 
    mysql_connect("localhost", "root", "") or die("Error connecting to database: ".mysql_error()); 

    mysql_select_db("waveevo") or die(mysql_error()); 
    ?> 


     <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
     <html xmlns="http://www.w3.org/1999/xhtml"> 
     <head> 
      <title>Search results</title> 
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
      <!-- <link rel="stylesheet" type="text/css" href="style.css"/> --> 
      <style> 
      table tr:nth-child(even) { 
       background-color: #eee; 
      } 
      table tr:nth-child(odd) { 
       background-color:#fff; 
      } 
      table th { 
       background-color: black; 
       color: white; 
      } 
      </style> 
     </head> 
     <body> 

    <? 

php 
    $query = $_GET['client_name']; 
    $query2 = $_GET['client_ic']; 
    $query3 = $_GET['client_add']; 

    if ($query == null && $query2 == null && $query3 == null) 
    { 
     echo "Please at least insert one the value"; 
    } 

    else 
    {  
     $query = htmlspecialchars($query); 
     $query2 = htmlspecialchars($query2); 
     $query3 = htmlspecialchars($query3); 

     $query = mysql_real_escape_string($query); 
     $query2 = mysql_real_escape_string($query2); 
     $query3 = mysql_real_escape_string($query3); 

     $raw_results = mysql_query("SELECT * FROM client WHERE ('client_name' LIKE '%".$query."%') OR ('client_ic' LIKE '%".$query2."%') OR ('client_add_1' && ' ' && 'client_add_2' && ' ' && 'client_add_3' && ' ' && 'client_add_4' LIKE '%".$query3."%')") or die(mysql_error());; 

     if(mysql_num_rows($raw_results) > null){ // if one or more rows are returned do following 

      while($results = mysql_fetch_array($raw_results)){ 
       ?> 
       <table width="100%"> 
        <tr> 
         <th>ID</th> 
         <th>Name</th> 
         <th>IC</th> 
         <th>Mobile</th> 
         <th>Address</th> 
         <th>Marital Status</th> 
         <th>Race</th> 
         <th>Asset Type</th> 
         <th>Bank</th> 
         <th>Amount</th> 
         <th>Nationality</th> 
         <th>Limit</th> 
        </tr> 
        <tr> 
         <td><?php echo $results["client_id"]; ?></td> 
         <td><?php echo $results["client_name"]; ?></td> 
         <td><?php echo $results["client_ic"]; ?></td> 
         <td><br><?php echo $results["client_mobile_1"]."<br>".$results["client_mobile_2"]."<br>".$results["client_mobile_3"]; ?></td> 
         <td><?php echo $results["client_add_1"]."<br>".$results["client_add_2"]."<br>".$results["client_add_3"]."<br>".$results["client_city"]."<br>".$results["client_postcode"]; ?></td> 
         <td><?php echo $results["client_marital_status_id"]; ?></td> 
         <td><?php echo $results["client_race_id"]; ?></td> 
         <td><?php echo $results["client_asset_type_id"]; ?></td> 
         <td><?php echo $results["client_bank_id"]; ?></td> 
         <td><?php echo $results["amount"]; ?></td> 
         <td><?php echo $results["client_nationality_id"]; ?></td> 
         <td><?php echo $results["client_limit"]; ?></td> 
        </tr> 
       </table> 

      <?php 
      } 

     } 



     else{ // if there is no matching rows do following 
      echo "No results"; 
     } 
    } 
?> 
</body> 

所以我嘗試輸入客戶端ic的值,例如1234,數據庫中沒有與我剛剛輸入的值相匹配的數據,但結果仍然顯示爲sho W的,我知道爲什麼,因爲我已經因爲當你使用通配符LIKE '%".$query."%'如果您的變量$query是空的,你是剛開始都是因爲你的一切LIKE '%%'比較沒有辦法來解決這個

+0

您使用通配符,所以基本上有可能是一個值的行是「12345」或「51234」並且會因爲查詢參數周圍的'%'字符而匹配。 MySQL不能組成數據,所以如果你得到一個結果,它意味着它以某種方式匹配。 – FMashiro

+0

另外,如果你提交一個空的字段,你基本上會運行一個查詢,說選擇這個列以任何東西開始的所有東西,並以任何結束。所以也可能有問題 – FMashiro

+0

client_ic的數據庫中的數據是1234,並且我搜索了99,顯示了一個結果。我的數據庫只有一個數據 – shaqirzul

回答

1

那。 您需要更改這句話:

$raw_results = mysql_query("SELECT * FROM client WHERE ('client_name' LIKE '%".$query."%') OR ('client_ic' LIKE '%".$query2."%') OR ('client_add_1' && ' ' && 'client_add_2' && ' ' && 'client_add_3' && ' ' && 'client_add_4' LIKE '%".$query3."%')") or die(mysql_error());; 

蒙山像這樣

$sql_query="SELECT * FROM client WHERE "; 
$other=false;  

if ($query != null and $query!="") { 
    $sql_query=$sql_query."('client_name' LIKE '%".$query."%')"; 
    $other=true; 
} 

if ($query2 != null and $query2!="") { 
    if ($other) { 
     $sql_query=$sql_query." OR ";  
    } 
    $sql_query=$sql_query."('client_ic' LIKE '%".$query2."%')"; 
    $other=true; 
} 
if ($query3 != null and $query3!="") { 
    if ($other) { 
     $sql_query=$sql_query." OR ";  
    } 
    $sql_query=$sql_query."('client_add_1' && ' ' && 'client_add_2' && ' ' && 'client_add_3' && ' ' && 'client_add_4' LIKE '%".$query3."%')"; 
    $other=true; 
}  
if ($other) { 
    $raw_results = mysql_query($sql_query) or die(mysql_error()); 
} 
+0

它仍然顯示結果,即使沒有結果也不應該顯示 – shaqirzul

+0

嘗試設置if($ query!= null和$ query!=「」)爲$ query,$ query2&$ query3以避免「」值 – nacho