Hello peeps!

I've got an issue which I am trying to sort out.
I am trying to build a website where ppl can search for car's sell and buy.

I got stack where I had to make the search option on the website, so I started looking on google where and how I can find solution on my problem so I found this webpage http://www.daniweb.com/web-development/php/threads/367225/filter-search-phpmysql .

I decided to use the code provided there so I can filter my search option and to finaly complete that, but after making some minor changes such as changing the database fields names and so on, now the code produces a error which says that the virable on line 51 is undefined and I can find out why... can you check it out for me please? As you can see from the issue of got I am not experienced with PHP at all, so would really appreaciate if you can help me out with this.

Here is the code I am talking about:

<?php


            $connect = mysql_connect("localhost","root","8707056341") or die ('Error: '.mysql_error());
            mysql_select_db("comparecars.co.uk"); //select database

if(isset($_GET['submit']) && !empty($_GET))
{           

            $where = "1 = 1";
            if($_POST['search_make']!="")   {
            $s[] = "`make` like '%" . mysql_real_escape_string($_POST['search_make']) . "%'";
            }

            if($_POST['search_model']!="")  {
            $s[] = "`model` like '%"    . mysql_real_escape_string($_POST['search_model']) . "%'";
            }

            if($_POST['minprice']!="" && $_POST['maxprice']!="")    {
            $s[] = "(`price` between "  . intval($_POST['minprice']) ." AND ".intval($_POST['maxprice']) .")";
            }

            if($_POST['fuel']!="")  {
            $s[] = "`fueltype` like '%" . mysql_real_escape_string($_POST['fuel']) . "%'";
            }

            if($_POST['year1']!="" && $_POST['year2']!="")  {
                $s[] = "(`registration` between "   . intval($_POST['year1']) ." AND ".intval($_POST['year2']) .")";
            }

            if($_POST['search_bodytype']!="")   {
            $s[] = "`bodytype` like '%" . mysql_real_escape_string($_POST['search_bodytype']) . "%'";
            }

            if($_POST['search_doors']!="")  {
            $s[] = "`doors` like '%"    . mysql_real_escape_string($_POST['search_doors']) . "%'";
            }

            if($_POST['search_transmission']!="")   {
            $s[] = "`transmission` like '%" . mysql_real_escape_string($_POST['search_transmission']) . "%'";
            }

            if($_POST['search_enginesize']!="") {
            $s[] = "`enginesize` like '%"   . mysql_real_escape_string($_POST['search_enginesize']) . "%'";
            }

            if(isset($s)){
            $where = "WHERE (" . implode(" AND ",$s).")";
            }
}
            $query_str = "SELECT * FROM offers {$where";
            echo $query_str;

?>

Recommended Answers

All 6 Replies

Hi,

Can you load the actual file of this script on your editor and tell us what is on line 51?

well it is an html form that takes the action from a separate .php file. The content of the php file is the one above and the html form is here

 <div id="content">
<form action="test.php" method="POST">
                        <p><font face="Arial, Helvetica, sans-serif">Make:</font>
                                <select name="make">
                                            <option>Make All</option>
                                            <option>Volvo</option>
                                            <option>Ford</option>
                                            <option>Audi</option>
                                            <option>BMW</option>
                                            <option>Alfa Romeo</option>
                                </select>
                            <font face="Arial, Helvetica, sans-serif">Model:</font>
                                <select name="model">
                                            <option>Select</option>
                                            <option>Escort</option>
                                            <option>Mondeo</option>
                                            <option>S40</option>
                                            <option>A4</option>
                                </select>    
                            <font face="Arial, Helvetica, sans-serif">Price:</font>
                                <select name="minprice">
                                            <option>Min Price</option>
                                            <option>Less than £500</option>
                                            <option>£1000</option>
                                            <option>£2000</option>
                                            <option>£3000</option>
                                </select>
                                <select name="maxprice">
                                            <option>Max Price</option>
                                            <option>£1000</option>
                                            <option>£2000</option>
                                            <option>£3000</option>
                                            <option>More than £3000</option>
                                </select></p>
                        <P><font face="Arial, Helvetica, sans-serif">Fuel Type:</font>
                                     <select name="fuel">
                                                <option>Any</option>
                                                <option>Petrol</option>
                                                <option>Diesel</option>
                                                <option>LPG</option>
                                                <option>Hybrid</option>
                                     </select>
                           <font face="Arial, Helvetica, sans-serif">Year:</font>
                               <select name="year1">
                                                <option>Year</option>
                                                <option>1990</option>
                                                <option>1991</option>
                                                <option>1992</option>
                                                <option>1993</option>
                                                <option>1994</option>
                                                <option>1995</option>
                                                <option>1996</option>
                                                <option>1997</option>
                                                <option>1998</option>
                                                <option>1999</option>
                                                <option>2000</option>
                                                <option>2001</option>
                                                <option>2002</option>
                                                <option>2003</option>
                                                <option>2004</option>
                                                <option>2005</option>
                                                <option>2006</option>
                                                <option>2007</option>
                                                <option>2008</option>
                                                <option>2009</option>
                                                <option>2010</option>
                                                <option>2011</option>
                                                <option>2012</option>
                          </select>
                                <select name="year2">
                                                <option>Year</option>
                                                <option>1990</option>
                                                <option>1991</option>
                                                <option>1992</option>
                                                <option>1993</option>
                                                <option>1994</option>
                                                <option>1995</option>
                                                <option>1996</option>
                                                <option>1997</option>
                                                <option>1998</option>
                                                <option>1999</option>
                                                <option>2000</option>
                                                <option>2001</option>
                                                <option>2002</option>
                                                <option>2003</option>
                                                <option>2004</option>
                                                <option>2005</option>
                                                <option>2006</option>
                                                <option>2007</option>
                                                <option>2008</option>
                                                <option>2009</option>
                                                <option>2010</option>
                                                <option>2011</option>
                                                <option>2012</option>
                                </select>
                                <font face="Arial, Helvetica, sans-serif">Body Type:</font></th>
                                <select name='body'>
                                                <option>Please Select</option>
                                                <option>Cabrio/Roadstar</option>
                                                <option>Estate Car</option>
                                                <option>Limousine</option>
                                                <option>Off-road vehicle/Pickup truck</option>
                                                <option>Small Car</option>
                                                <option>Sport Car/Coupe</option>
                                                <option>Van/Minibus</option>
                                                <option>Other</option>
                                </select></P>
      <p><font face="Arial, Helvetica, sans-serif">Number of doors:</font>
                                <select name='doors'>
                                                <option>Any</option>
                                                <option>2</option>
                                                <option>3</option>
                                                <option>4</option>
                                                <option>5</option>
                                </select>      
                           <font face="Arial, Helvetica, sans-serif">Transmission Types:</font>
                                <select name='transmission'>
                                                <option>Any</option>
                                                <option>Manuel</option>
                                                <option>Automatic</option>
                                                <option>CVT</option>
                                                <option>Semi Automatic</option>
                                                <option>Tip Tronic</option>
                                                <option>DSG</option>  
                                </select>
                           <font face="Arial, Helvetica, sans-serif">Engine Size:</font></th>
                                <select name="engine">
                                                <option>1.0</option>
                                                <option>1.1</option>
                                                <option>1.2</option>
                                                <option>1.3</option>
                                                <option>1.4</option>
                                                <option>1.5</option>
                                                <option>1.6</option>
                                                <option>1.7</option>
                                                <option>1.8</option>
                                                <option>1.9</option>
                                                <option>2.0</option>
                                                <option>2.2</option>
                                                <option>2.4</option>
                                                <option>2.6</option>
                                                <option>2.8</option>
                                                <option>3.0</option>
                                                <option>3.2</option>
                                                <option>4.2</option>
                                </select></p>                 
                        <p><input type="submit" name="submit" value="Search" /></p>
                        </form> 
        </div>

I am trying to create a form that will search my database

and will have some filters to control the search. When you are looking for a car for example, I want only those records of the table to appear which match the search criterias. Otherwise, I know how to display all the records from the data, but this isn't what I am looking for in this case. Hope that makes sence! Thanks! :)

and this is the error that appears on my browser "Notice: Undefined variable: where in C:\xampp\htdocs\xampp\comparecars.co.uk\menu\test.php on line 51
SELECT * FROM offers"

ok, thanks for posting your codes.. now I need to see how your database table are structured. This will be the target database for your search query. I am expecting something like this..

Datatbase Name: Automobile
tables:
vehicle =>columns : id --- name -- title -- year -- model -- poster --- date posted -- etc..etc.

If you another table/s to list just list it here..

ok! here it is:
Database Name: comparecar.co.uk
table: offers
Fields: id -- make -- model -- registration -- fueltype -- enginesize -- power -- doors -- bodytype -- transmission -- colour -- usage -- mileage -- description -- price.
*Note: there are some more tables in the same database, but I do not know, if they play any role at this matter.

Another thing, which you may want to know (I don't know it makes any difference or not) but I'm using Dreamweaver.
Also, I managed to show the data as a whole, but when I try to put filtter in the code so it can filter my search, it doesn't show any mistakes, but a blank page. :(

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.