2013-11-21 103 views
0

在问任何事情之前,我想说我几乎没有任何经验,我对PHP非常陌生。我制作了数据库表,其中涉及有关在船上完成工作的信息/细节。PHP MySQL搜索引擎多输入

我的代码:

<?php 

error_reporting(0); 

require 'db/connect.php'; 
include 'header.php'; 
require 'functions/safety.php'; 

$records = array(); 

$user = trim($_POST['user']); 
$customer = trim($_POST['customer']); 
$vessel   = trim($_POST['vessel']); 
$country = trim($_POST['country']); 
$port = trim($_POST['port']); 
$eta = trim($_POST['eta']); 
$service_station = trim($_POST['service_station']); 
$type_of_service = trim($_POST['type_of_service']); 
$case_reference = trim($_POST['case_reference']); 
$status = trim($_POST['status']); 

if(isset($_POST['search'])) { 
    if($results = $db->query("SELECT * FROM pelates 
          WHERE user = '{$user}' 
          OR customer = '{$customer}' 
          OR vessel = '{$vessel}' 
          OR country = '{$country}' 
          OR port = '{$port}' 
          OR eta = '{$eta}' 
          OR service_station = '{$service_station}' 
          OR type_of_service = '{$type_of_service}' 
          OR case_reference = '{$case_reference}' 
          OR status = '{$status}'")) { 
     if($results->num_rows) { 
      while($row = $results->fetch_object()) { 
       $records[] = $row; 
      } 
      $results->free(); 
     } 
    } 
} 

?> 

<!DOCTYPE html> 

<html> 
<head> 
    <title>Search jobs</title> 
    <link rel="stylesheet" type="text/css" href="syles.css"> 
    <meta charset="utf-8" /> 
</head> 
<body> 

    <table> 
     <thead> 
      <tr> 
       <th>User</th> 
       <th>Customer</th> 
       <th>Vessel</th> 
       <th>Country</th> 
       <th>Port</th> 
       <th>Eta</th> 
       <th>Service station</th> 
       <th>Type of service</th> 
       <th>Case reference</th> 
       <th>Status</th> 
      </tr> 
     </thead> 
     <tbody> 
      <?php 
       foreach($records as $r) { 
       ?> 
        <tr> 
         <td class="green"><?php echo escape($r->user); ?></td> 
         <td class="blue"><?php echo escape($r->customer); ?></td> 
         <td class="green"><?php echo escape($r->vessel); ?></td> 
         <td class="blue"><?php echo escape($r->country); ?></td> 
         <td class="green"><?php echo escape($r->port); ?></td> 
         <td class="blue"><?php echo escape($r->eta); ?></td> 
         <td class="green"><?php echo escape($r->service_station); ?></td> 
         <td class="blue"><?php echo escape($r->type_of_service); ?></td> 
         <td class="green"><?php echo escape($r->case_reference); ?></td> 
         <td class="blue"><?php echo escape($r->status); ?></td> 
        </tr> 
       <?php 
       } 
       ?> 
     </tbody> 
    </table> 

    </br><a href="http://prinseapals-marine.com/filing/search.php" id="clear_button" class="button">Clear all</a> 

    <hr> 

    <form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post"> 
     <div class="field"> 
      <label for="user">User</label> 
      <select name="user" id="user"> 
       <option value="">Any</option> 
       <option value="VAK">VAK</option> 
       <option value="DTS">DTS</option> 
       <option value="SVAK">SVAK</option> 
      </select> 
     </div> 
     <div class="field"> 
      <label for="customer">Customer</label> 
      <input type="text" name="customer" id="customer" autocomplete="off"> 
     </div> 
     <div class="field"> 
      <label for="vessel">Vessel</label> 
      <input type="text" name="vessel" id="vessel" autocomplete="off"> 
     </div> 
     <div class="field"> 
      <label for="country">Country</label> 
      <input type="text" name="country" id="country" autocomplete="off"></input> 
     </div> 
     <div class="field"> 
      <label for="port">Port</label> 
      <input type="text" name="port" id="port" autocomplete="off"> 
     </div> 
     <div class="field"> 
      <label for="eta">ETA</label> 
      <input type="text" name="eta" id="eta" autocomplete="off"> 
     </div> 
     <div class="field"> 
      <label for="service_station">Service station</label> 
      <input type="text" name="service_station" id="service_station" autocomplete="off"> 
     </div> 
     <div class="field"> 
      <label for="type_of_service">Type of service</label> 
      <input type="text" name="type_of_service" id="type_of_service" autocomplete="off"> 
     </div> 
     <div class="field"> 
      <label for="case_reference">Case reference</label> 
      <input type="text" name="case_reference" id="case_reference" autocomplete="off"> 
     </div> 
     <div class="field" id="radio"> 
      <label>Pending</label> 
      <input type="radio" name="status" value="pending"/></br> 
      <label>Offer</label> 
      <input type="radio" name="status" value="offer"/></br> 
      <label>Order</label> 
      <input type="radio" name="status" value="order"/></br> 
      <label>Lost</label> 
      <input type="radio" name="status" value="lost"/></br> 
      <label>Postponed</label> 
      <input type="radio" name="status" value="postponed"/></br> 
      <label>Declined</label> 
      <input type="radio" name="status" value="declined"/> 
     </div> 
     <input type="submit" value="Search database" class="button" name="search"> 
    </form> 

</body> 
</html> 

我的问题是,由于在$db->query("SELECT...")我使用或我的变量之间当我尝试搜索用户=史蒂芬和状态=例如待定,能把我所有的结果将Steven作为用户或Pending作为状态,而不是将Steven和Pending作为用户和状态的结果。

所以我试着使用和我的变量之间,然后我有另一个问题。比方说,我再次搜索user = Steven和status = Pending。问题在于,因为我只填写了10个可用搜索输入中的2个,并且因为我的数据库中的任何字段中没有任何值,所以我没有得到任何结果,因为程序正在以Steven作为用户搜索作业,作为状态,毫无作为的国家,毫无作为容器的推移...

我知道我的问题可能是愚蠢的或太容易回答,但请原谅我,因为我在一开始我真的很新的PHP提到的一样4天 - 新。如果有人能帮我解决这个问题,我会很感激。

在此先感谢。

回答

0

我建议您有条件地从$_POST数据中填充变量,以便每个变量在空白时都将设置为false。然后,排除SQL查询中的错误值。

这样,您的查询将只包含已填充到表单中的值。留空的输入不会影响查询。

事情是这样的:

// build array of field names 
$fields=array('user','customer','vessel','country', 
      'port','eta','service_station','type_of_service', 
      'case_reference','status'); 


// initialize empty array for WHERE clauses 
$wheres=array(); 

// loop through field names, get POSTed values, 
// and build array of WHERE clauses, excluding false values 
foreach ($fields as $field) { 

    // get existing field value from POST, mark missing or empty value as FALSE 
    ${$field} = isset($_POST[$field]) && trim($_POST[$field])!='' 
     ? trim($_POST[$field]) : false; 

    // add to array of WHERE clauses only if value is not FALSE 
    if (${$field}) { $wheres[]="$field='".${$field}."'"; } 

} 

// build SELECT statement from WHERE clauses 
$sql="SELECT * FROM pelates WHERE ". 
    (!empty($wheres) ? implode(" AND ",$wheres) : '1=1'). 
    ";"; 

// preview your query on screen 
echo "<p>$sql</p>"; 
+0

您的解决方案就像一个魅力我为您快速回复真的很感谢。但是因为正如我上面提到的,我真的是PHP新手,我无法理解它(尽管我必须承认你真的试图解释)。事实上,你的答案让我觉得不好,甚至试图让我想要的Web应用程序:(再次感谢:P –

+0

很高兴我可以帮助。不要觉得不好,这是很好的学习。如果有什么令你困惑,但你想理解,随时提问。 – showdev