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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?