PHP MySQL 搜索引擎多输入

标签 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...") 中,当我尝试搜索 user=Steven 和 status=Pending 时,我在变量之间使用了 OR ,它为我提供了以 Steven 为用户或以 Pending 作为状态的所有结果,而不是以 Steven AND Pending 作为用户和状态的结果。

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

我知道我的问题可能很愚蠢或者太容易回答,但请原谅我,因为正如我在开头提到的,我对 php 真的很陌生,就像 4 天新的一样。如果有人能帮助我解决这些问题,我将非常感激。

提前致谢。

最佳答案

我建议您有条件地从 $_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>";

关于PHP MySQL 搜索引擎多输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20110005/

相关文章:

javascript - 使用 ajax 上下文并不能解决范围问题

search - Rails 3 搜索 : Searchlogic vs Thinking_Sphinx

algorithm - 如何找到已排序列表的子集的边界?

javascript - 使用 POST 处理表单

php - 当数据库中有两个日期时,如何按日期 DESC 查询 ORDER?

php - Mysqli 在多个字段中搜索值

MySQL 如何将多组数据和列连接到一张表中

mysql - 在 mysql select 语句中重用内部查询

mysql - 身份验证插件 'caching_sha2_password'无法加载

css - 样式搜索栏和转到按钮没有间隙