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