Mysql Select All

advertisements

In my database there are some vehicles with different categories and types. I want to create a form to filter vehicles by categories and types. So the form codes are as following.

<form action="" method="post">
Type <select name="search_type">
           <option value="jeep">Jeep</option>
           <option value="Wagon">Wagon</option>
           <option value="cab">Cab</option>
     </select>

Category <select name="search_category">
           <option value="Recondition">Recondition</option>
           <option value="Used">Used</option>
     </select>
    <input type="submit" name="search_submit"></form>

Here are the php code:

if(isset($_POST['search_submit']))
{
  $all_vehicles    = FALSE;
  $search_type     = $_POST['search_type'];
  $search_category = $_POST['search_category'];
  $search_brand    = $_POST['search_brand'];

  $search_result   = mysql_query("SELECT * FROM vehicle_info WHERE type='$search_type' && catagory='$search_catagory' && brand='$search_brand' ORDER BY id desc");
}
else
{
  $search_result = mysql_query("SELECT * FROM vehicle_info ORDER BY id desc");
}

If someone wants to search jeeps with any category (used or recondition), what code I should write in the query?

I know I can add a new <option> as "ANY". But I don't know how to use it in the query.


You dont have to store it as any. Just select it without the category. Assuming these are the only two categories stored, of course.

SELECT * FROM vehicle_info WHERE type = '$search_type' ORDER BY id DESC;

Also; please pay attention to SQL injections.


Edit in response at @dualed:

Correct. He could simply add <option value="all">All categories</option>

And add an if-statement:

if ($search_category != "all")
{
    $search_result = mysql_query("SELECT * FROM vehicle_info WHERE type = '$search_type' AND category = '$search_category' ORDER BY id DESC;");
}