doujiaohuo1096 2014-10-15 00:37
浏览 37
已采纳

使用多个PHP下拉列表来过滤MySQL表数据

I am currently building a page that uses five separate drop down menus (static content) that when selected the combination of what is selected will filter my mysql query and display the correct results using those parameters.

I am a newbie in php and i don't know how to go about it

Here is the html code for index.php

<form action="search.php" method="post">
<p>Body Type</p>
                <select class="form-control" name="bodytype">
                    <option value="" selected="selected">Any body type</option>
                     <option value="saloon">Saloons</option>
                    <option value="hatchback">Hatchbacks</option>
                    <option value="4 wheel drive">4 wheel drives</option>
                <option value="station wagon">Station wagon</option>
                <option value="pickup">Pickups</option>
                    <option value="motor bike">Motor bikes</option>
                <option value="convertible">Convertibles</option>
                <option value="bus">Buses, Danfos & Vans</option>
                <option value="truck">Trucks</option>
                </select>


<p>Condition</p>
                <select class="form-control" name="condition">
                    <option value="" selected="selected">Any condition</option>
                     <option value="brand new">Brand new</option>
                    <option value="foreign used">Foreign used (Tokunbo)</option>
                    <option value="nigerian used">Nigerian used (Registered)</option>
                 </select>



<p>Fuel type</p>
                <select class="form-control" name="fueltype">
                    <option value="" selected="selected">Any fuel type</option>
                     <option value="petrol">Petrol</option>
                    <option value="diesel">Diesel</option>
                    <option value="hybrid">Hybrid</option>   
                 </select>


<p>Transmission</p>
                <select class="form-control" name="transmission">
                    <option value="" selected="selected">Any transmission</option>
                     <option value="automatic">Automatic</option>
                    <option value="manual">Manual</option>
                 </select>


<p>Drive type</p>
                <select class="form-control" name="drivetype">
                    <option value="" selected="selected">Any drive type</option>
                     <option value="2 wheel drive">2 wheel drive</option>
                    <option value="4 wheel drive">4 wheel drive</option>
                 </select>

<input name="search" type="submit" value="Search">
</form>`

This is the search.php

<body>

<?php
$selected_btype = $_POST['bodytype'];  // Storing Selected Value In Variable
$selected_condition = $_POST['condition'];
$selected_fueltype = $_POST['fueltype'];
$selected_makemodel = $_POST['makemodel'];
$selected_transmission = $_POST['transmission'];
$selected_location = $_POST['location'];
$selected_dtype = $_POST['drivetype'];
$selected_year= $_POST['year'];
$selected_dsetup= $_POST['drivesetup'];


$query="SELECT * FROM vehicle WHERE(body_type=$selected_btype) OR
(condition='$selected_condition') OR (fuel_type='$selected_fueltype') OR
(make_model='$selected_makemodel') OR (transmission='$selected_transmission') OR
(location='$selected_location') OR (drive_type='$selected_dtype') OR
(year='$selected_year') OR (drive_setup='$selected_dsetup')";


$result=mysql_query($query);
if($result)
{
while ($row=mysql_fetch_array($result)){
echo "<tr>";
echo "<td>".$row['sn']."</td>"; 
echo "<td>".$row['body_type']."</td>";  
echo "<td>".$row['make_model']."</td>"; 
echo "<td>".$row['location']."</td></tr>"; 
echo "<td>".$row['year']."</td>"; 
echo "<td>".$row['condition']."</td>"; 
echo "<td>".$row['transmission']."</td>"; 
echo "<td>".$row['description']."</td></tr>"; 
echo "<td>".$row['images']."</td></tr>"; 
}
}else{
die(mysql_error());
}
?>

The output of search.php:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'condition='nigerian used') OR (fuel_type='') OR (make_model='peugeot'' at line 1

  • 写回答

2条回答 默认 最新

  • duanci8209 2014-10-15 01:13
    关注

    There are a few issues with your code:

    Firstly, condition is a MySQL reserved word and needs to be wrapped in backticks.

    Either wrap it in backticks or rename it to another word.

    $query="SELECT * FROM vehicle WHERE(body_type=$selected_btype) OR
    (`condition`='$selected_condition')
    

    Notice where the error is pointing to:

    for the right syntax to use near 'condition='nigerian used')  
                                     ^
    

    Then there's WHERE(body_type=$selected_btype)

    The $selected_btype variable needs to be wrapped in quotes, since string values are involved.

    WHERE(body_type='$selected_btype')
    

    Footnotes:

    You should consider using mysqli with prepared statements, or PDO with prepared statements, they're much safer, because your present code is open to SQL injection.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看