php高级搜索初学者

时间:2015-10-15 18:54:18

标签: php mysqli

我有下面显示的搜索结果表单,它会产生如下所示的结果。如果我只想显示CustomerId 8888的结果,而不显示任何其他CustomerId的结果,那么表单中必须更改哪些内容?

搜索表单包含列出的所有列的输入字段(CustomerIdComplaintIdCompany,...)。

<table class="table table-striped" > 
    <p>

        <tr>
            <th>CustomerID</th>
            <th>ComplaintID</th>
            <th>Company</th>
            <th>Lastname</th>
            <th>Firstname</th>
            <th>Street</th>
            <th>Nr</th>
            <th>Postalcode</th>
            <th>City</th>
            <th>Phone</th>
            <th>Mail</th>
        <tr>


    <?php 
    error_reporting(-1);
    ini_set('display_errors', true);

    include "config.php";




    $customerid = mysqli_real_escape_string($mysqli, $_GET['customerid']);
    $complaintid = mysqli_real_escape_string($mysqli, $_GET['complaintid']);
    $company = mysqli_real_escape_string($mysqli, $_GET['company']);
    $lastname = mysqli_real_escape_string($mysqli, $_GET['lastname']);
    $firstname = mysqli_real_escape_string($mysqli, $_GET['firstname']);
    $street = mysqli_real_escape_string($mysqli, $_GET['street']);
    $housenr = mysqli_real_escape_string($mysqli, $_GET['housenr']);
    $postalcode = mysqli_real_escape_string($mysqli, $_GET['postalcode']);
    $city = mysqli_real_escape_string($mysqli, $_GET['city']);
    $phone = mysqli_real_escape_string($mysqli, $_GET['phone']);
    $mail = mysqli_real_escape_string($mysqli, $_GET['mail']);

    $result = $mysqli->query("SELECT customerid, complaintid, company, lastname, firstname, street, housenr, postalcode, city, phone , mail 
                                    FROM customer
                                    WHERE
                                    customerid = '".$customerid."' OR
                                    complaintid = '".$complaintid."' OR
                                    company = '".$company."' OR
                                    lastname = '".$lastname."' OR
                                    firstname = '".$firstname."' OR
                                    street = '".$street."' OR
                                    housenr = '".$housenr."' OR
                                    postalcode = '".$postalcode."' OR
                                    city = '".$city."' OR
                                    phone = '".$phone."' OR
                                    mail = '".$mail."' ");


    while ($row = $result->fetch_assoc()):
    ?>

    <tr>
        <td><?php echo $row['customerid']; ?></td>
        <td><?php echo $row['complaintid']; ?></td>
        <td><?php echo $row['company']; ?></td>
        <td><?php echo $row['lastname']; ?></td>
        <td><?php echo $row['firstname']; ?></td>
        <td><?php echo $row['street']; ?></td>
        <td><?php echo $row['housenr']; ?></td>
        <td><?php echo $row['postalcode']; ?></td>
        <td><?php echo $row['city']; ?></td>
        <td><?php echo $row['phone']; ?></td>
        <td><?php echo $row['mail']; ?></td>
    </tr>

    <?php
    endwhile;
    ?>



        <table>
        <tr>
            <th><a class="btn btn-info" href="index.php" role="button">zurück zur Suche</a></th>
        </tr>
        </table>

结果现在是这样的: sample

1 个答案:

答案 0 :(得分:2)

您的SELECT查询错误,只能根据customerID获得所需的结果。

$result = $mysqli->query("SELECT kdnr, complaintid, company, lastname, firstname, street, housenr, postalcode, city, phone , mail 
                                FROM customer
                                WHERE
                                kdnr = '".$kdnr."' OR
                                complaintid = '".$complaintid."' OR
                                company = '".$company."' OR
                                lastname = '".$lastname."' OR
                                firstname = '".$firstname."' OR
                                street = '".$street."' OR
                                housenr = '".$housenr."' OR
                                postalcode = '".$postalcode."' OR
                                city = '".$city."' OR
                                phone = '".$phone."' OR
                                mail = '".$mail."' ");

需要只是

$result = $mysqli->query("SELECT kdnr, complaintid, company, lastname, firstname, street, housenr, postalcode, city, phone , mail 
                                FROM customer
                                WHERE
                                kdnr = '".$kdnr."'");

我假设$kdnr是customerID变量。 OR语句正在获取其他记录,因为它们符合OR标准之一。