PHP MySQL搜索引擎多输入

时间:2013-11-21 00:24:18

标签: php mysql search

在提出任何问题之前我想说的是我对php非常非常新,几乎没有经验。我已经制作了数据库表,其中包含有关在船上完成工作的信息/详细信息。

我的代码:

<?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...")我在我的变量之间使用OR时,当我尝试搜索user = Steven并且status = Pending时,它会让我获得Steven作为用户或待定的所有结果作为状态,而不是具有Steven和Pending作为用户和状态的结果。

所以我尝试在我的变量之间使用AND,然后我遇到了另一个问题。假设我再次搜索user = Steven和status = Pending。问题是,因为我只从10个可用的搜索输入中填写2,因为我的数据库中的任何字段都没有任何NOTHING值,因为程序正在以Steven作为用户搜索作业,因此没有结果状态,没有作为国家,没有作为船只,继续......

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

提前致谢。

1 个答案:

答案 0 :(得分: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>";
相关问题