PHP搜索查询未显示结果

时间:2018-11-06 02:29:09

标签: php sql

我正在尝试使用一种表格来搜索汽车数据库。当有人选择“任何”时,我希望表格显示该字段的所有选项,但是当有人选择特定选项时,我希望表格显示仅具有该选项值的汽车。下面的代码显示了到目前为止的内容,但未显示任何结果。任何建议将不胜感激。谢谢。

carSearch.php

    <form id="searchForm" class="form" method="POST" action="results.php">
                <div class="form-group">
                    <label for="make" class="mr-sm-2">Make:</label>
                    <select class="form-control mb-2 mr-sm-2" name="make" id="make"></select>
                </div>                    
                <div class="form-group">
                    <label for="model" class="mr-sm-2">Model:</label>
                    <select class="form-control mb-2 mr-sm-2" name="model" id="model"></select>
                </div>                    
                <div class="form-group">
                    <label for="bodyType" class="mr-sm-2">Body Type:</label>
                    <select class="form-control mb-2 mr-sm-2" name="bodyType" id="bodyType">
                        <option value="`Body Type`">Any</option>
                        <option value="Convertible">Convertible</option>
                        <option value="Coupe">Coupe</option>
                        <option value="Hatch">Hatch</option>
                        <option value="Sedan">Sedan</option>
                        <option value="SUV">SUV</option>
                        <option value="Wagon">Wagon</option>
                    </select>
                </div>                       
                <div class="form-group">
                    <label for="minPrice" class="mr-sm-2">Minimum Price:</label>
                    <select class="form-control mb-2 mr-sm-2" name="minPrice" id="minPrice">
                        <option value="50000">Any</option>
                        <option value="50000">$50,000</option>
                        <option value="100000">$100,000</option>
                        <option value="150000">$150,000</option>
                        <option value="200000">$200,000</option>
                        <option value="250000">$250,000</option>
                        <option value="300000">$300,000</option> 
                        <option value="350000">$350,000</option>
                        <option value="400000">$400,000</option> 
                        <option value="450000">$450,000</option>
                        <option value="500000">$500,000</option> 
                        <option value="550000">$550,000</option>
                        <option value="600000">$600,000</option> 
                        <option value="650000">$650,000</option>
                        <option value="700000">$700,000</option> 
                        <option value="750000">$750,000</option>
                        <option value="800000">$800,000</option>  
                        <option value="850000">$850,000</option>  
                        <option value="900000">$900,000</option>                             
                    </select>
                </div>                     
                <div class="form-group">
                    <label for="maxPrice" class="mr-sm-2">Maximum Price:</label>
                    <select class="form-control mb-2 mr-sm-2" name="maxPrice" id="maxPrice">
                        <option value="900000">Any</option>
                        <option value="50000">$50,000</option>
                        <option value="100000">$100,000</option>
                        <option value="150000">$150,000</option>
                        <option value="200000">$200,000</option>
                        <option value="250000">$250,000</option>
                        <option value="300000">$300,000</option> 
                        <option value="350000">$350,000</option>
                        <option value="400000">$400,000</option> 
                        <option value="450000">$450,000</option>
                        <option value="500000">$500,000</option> 
                        <option value="550000">$550,000</option>
                        <option value="600000">$600,000</option> 
                        <option value="650000">$650,000</option>
                        <option value="700000">$700,000</option> 
                        <option value="750000">$750,000</option>
                        <option value="800000">$800,000</option>  
                        <option value="850000">$850,000</option>  
                        <option value="900000">$900,000</option>                            
                    </select>
                </div>                       
                <div class="form-group">
                    <label for="driveType" class="mr-sm-2">Drive Type:</label>
                    <select class="form-control mb-2 mr-sm-2" name="driveType" id="driveType">
                        <option value="`Drive Type`">Any</option>
                        <option value="4WD">4WD</option>
                        <option value="AWD">AWD</option>
                        <option value="RWD">RWD</option>
                    </select>
                </div>              
                <div class="form-group">
                    <label for="doors" class="mr-sm-2">Doors:</label>
                    <select class="form-control mb-2 mr-sm-2" name="doors" id="doors">
                        <option value="`Doors`">Any</option>
                        <option value="2">2</option>
                        <option value="4">4</option>
                        <option value="5">5</option>
                        <option value="7">7</option>
                    </select>
                </div>                    
                <div class="form-group">
                    <label for="seats" class="mr-sm-2">Seats:</label>
                    <select class="form-control mb-2 mr-sm-2" name="seats" id="seats">
                        <option value="`Seats`">Any</option>
                        <option value="2">2</option>
                        <option value="4">4</option>
                        <option value="5">5</option>
                        <option value="7">7</option>
                    </select>
                </div>                    
                <div class="form-group">
                    <label for="fuelType" class="mr-sm-2">Fuel Type:</label>
                    <select class="form-control mb-2 mr-sm-2" name="fuelType" id="fuelType">
                        <option value="`Fuel Type`">Any</option>
                        <option value="Petrol">Petrol</option>
                        <option value="Diesel">Diesel</option>
                        <option value="Hybrid">Hybrid</option>
                        <option value="Electric">Electric</option>
                    </select>
                </div>                  
                <div class="form-group">
                    <label for="cylinders" class="mr-sm-2">Cylinders:</label>
                    <select class="form-control mb-2 mr-sm-2" name="cylinders" id="cylinders">
                        <option value="`Cylinders`">Any</option>
                        <option value="3">3</option>
                        <option value="4">4</option>
                        <option value="5">5</option>
                        <option value="6">6</option>
                        <option value="8">8</option>
                        <option value="10">10</option>
                        <option value="12">12</option>
                    </select>
                </div>                    
                <div class="form-group">
                    <label for="minEngineSize" class="mr-sm-2">Minimum Engine Size:</label>
                    <select class="form-control mb-2 mr-sm-2" name="minEngineSize" id="minEngineSize">
                        <option value="1000">Any</option>
                        <option value="1000">1,000</option>
                        <option value="2000">2,000</option>
                        <option value="3000">3,000</option>
                        <option value="4000">4,000</option>
                        <option value="5000">5,000</option>
                        <option value="6000">6,000</option>
                        <option value="7000">7,000</option>
                    </select>
                </div>                    
                <div class="form-group">
                    <label for="maxEngineSize" class="mr-sm-2">Maximum Engine Size:</label>
                    <select class="form-control mb-2 mr-sm-2" name="maxEngineSize" id="maxEngineSize">
                        <option value="7000">Any</option>
                        <option value="1000">1,000</option>
                        <option value="2000">2,000</option>
                        <option value="3000">3,000</option>
                        <option value="4000">4,000</option>
                        <option value="5000">5,000</option>
                        <option value="6000">6,000</option>
                        <option value="7000">7,000</option>
                    </select>
                </div>
                <button type="submit" class="btn btn-danger mb-2 submitButton">GO! >></button>
            </form>

results.php

    <table id="carsTable">
                    <?php
                    $mysqli = new mysqli("localhost", "***", "***", "***");


                        if ($mysqli->connect_errno) {
                            printf("Connect failed: %s\n", $mysqli->connect_error);
                            exit();
                        }

                        $make = mysqli_real_escape_string($mysqli, $_POST['make']);
                        $model = mysqli_real_escape_string($mysqli, $_POST['model']);
                        $bodyType = mysqli_real_escape_string($mysqli, $_POST['bodyType']);
                        $minPrice = mysqli_real_escape_string($mysqli, $_POST['minPrice']);
                        $maxPrice = mysqli_real_escape_string($mysqli, $_POST['maxPrice']);
                        $driveType = mysqli_real_escape_string($mysqli, $_POST['driveType']);
                        $doors = mysqli_real_escape_string($mysqli, $_POST['doors']);
                        $seats = mysqli_real_escape_string($mysqli, $_POST['seats']);
                        $fuelType = mysqli_real_escape_string($mysqli, $_POST['fuelType']);
                        $cylinders = mysqli_real_escape_string($mysqli, $_POST['cylinders']);
                        $minEngineSize = mysqli_real_escape_string($mysqli, $_POST['minEngineSize']);
                        $maxEngineSize = mysqli_real_escape_string($mysqli, $_POST['maxEngineSize']);

                        if ($make = "Any") {
                            $make = "`Make`";
                        }
                        if ($model = "Any") {
                            $model = "`Model`";
                        }

                        /*Create table doesn't return a resultset*/ 
                        if ($mysqli->query("CREATE TEMPORARY TABLE kenscars LIKE cars") === FALSE) {
                            printf("Creation of Table Cars Unsuccessful.\n");
                        }

                        $sql = "SELECT * FROM cars WHERE `Make`='$make' AND `Model`=$model AND `Body Type`=$bodyType AND (`Price` BETWEEN $minPrice AND $maxPrice) AND `Drive Type`=$driveType AND `Doors`=$doors AND `Seats`=$seats AND `Fuel Type`=$fuelType AND `Cylinders`=$cylinders AND (`Engine Size` BETWEEN $minEngineSize AND $maxEngineSize) ORDER BY `Make`, `Model`";
                        $results = $mysqli->query($sql);
                        if (!$results) {
                            printf("Error: %s\n", mysqli_error($mysqli));
                            exit();
                        }
                        while($row = mysqli_fetch_array($results)) {
                    ?>
                        <tr>
                            <td style="text-align:center;">
                                <strong><span class="carTitle"><?php echo $row['Year']?> <?php echo $row['Make']?> <?php echo $row['Model']?></span></strong>
                                <br>
                                <br>
                                <img src="Images/Cars/<?php echo $row['Image']?>" width="80%">
                                <br>
                                <br>
                                <form id="enquireForm" class="form" method="POST" action="carEnquiry.php">
                                    <input type="hidden" name="enquiryCar" id="enquiryCar" value="<?php echo $row['Year']?> <?php echo $row['Make']?> <?php echo $row['Model']?>">
                                    <button type="submit" class="btn btn-danger btn-block mb-2">ENQUIRE NOW! >></button>
                                </form>
                            </td>
                            <td>
                                <table id="innerCarsTable">
                                    <tr>
                                        <th class="header">Price:</th>
                                        <td>$<?php echo $row['Price']?></td>
                                    </tr>
                                    <tr>
                                        <th class="header">Body Type:</th>
                                        <td><?php echo $row['Body Type']?></td>
                                    </tr>
                                    <tr>
                                        <th class="header">Drive Type:</th>
                                        <td><?php echo $row['Drive Type']?></td>
                                    </tr>
                                    <tr>
                                        <th class="header">Fuel Type:</th>
                                        <td><?php echo $row['FuelType']?></td>
                                    </tr>
                                    <tr>
                                        <th class="header">Fuel Efficiency:</th>
                                        <td><?php echo $row['Fuel Efficiency']?>L/100km</td>
                                    </tr>
                                    <tr>
                                        <th class="header">Transmission:</th>
                                        <td><?php echo $row['Transmission']?></td>
                                    </tr>
                                    <tr>
                                        <th class="header">Doors:</th>
                                        <td><?php echo $row['Doors']?></td>
                                    </tr>
                                    <tr>
                                        <th class="header">Seats:</th>
                                        <td><?php echo $row['Seats']?></td>
                                    </tr>
                                    <tr>
                                        <th class="header">Cylinders:</th>
                                        <td><?php echo $row['Cylinders']?></td>
                                    </tr>
                                    <tr>
                                        <th class="header">Engine Capacity:</th>
                                        <td><?php echo $row['Engine Capacity']?>L</td>
                                    </tr>
                                    <tr>
                                        <th class="header">Engine Size:</th>
                                        <td><?php echo $row['Engine Size']?>cc</td>
                                    </tr>
                                    <tr>
                                        <th class="header">Power:</th>
                                        <td><?php echo $row['Power']?></td>
                                    </tr>
                                    <tr>
                                        <th class="header">Torque:</th>
                                        <td><?php echo $row['Torque']?></td>
                                    </tr>
                                </table>
                                <br>
                            </td>
                        </tr>

                    <?php
                    }                        
                    ?>
                </table>

0 个答案:

没有答案