I'm an absolute noob to programming and over the past 10 days or so I have been building a 'buy sell trade' website for things.
Anyway I have got to the point where I now have the ability for people to register, post ads and reply to them... but now I want to have a page where they can search through the adverts.
The search form works on 3 fields, 'make', 'model' and 'caliber' (site is for guns)
<form action="" method="get" autocomplete="off">
I'm looking for a
<input type="text" name="make" class="autosuggestmake" placeholder="Manufacturer"/>
<div class="dropdown">
<ul class="resultmake"></ul>
</div>
<input type="text" name="model" class="autosuggestmodel" placeholder="Model"/>
<div class="dropdown">
<ul class="resultmodel"></ul>
</div>
in
<select name="caliber" >
<option value="*">(Caliber) Any</option>
<option value=".177">.177</option>
<option value=".20">.20</option>
<option value=".22">.22</option>
<option value=".25">.25</option>
</select>
<input type="submit" value="Search" />
This is posted as GET data which I 'catch' with this code:
$advert = new Advert;
if (empty($_GET) === true){
$adverts = $advert->fetch_all();
} else {
$search_make = $_GET['make'];
$search_model = $_GET['model'];
$search_caliber = $_GET['caliber'];
$adverts = $advert->fetch_results($search_make, $search_model, $search_caliber);
}
My fetch_results query is this:
class Advert {
public function fetch_results($search_make, $search_model, $search_caliber) {
global $pdo;
$search_caliber = mysql_real_escape_string($search_caliber);
$search_make = mysql_real_escape_string($search_make);
$search_model = mysql_real_escape_string($search_model);
if (empty($search_make) === true){
$search_make = "*";
}
if (empty($search_model) === true){
$search_model = "*";
}
$query = $pdo -> prepare("SELECT * FROM `adverts` WHERE make = '$search_make' AND model = '$search_model' AND caliber = '$search_caliber'");
$query -> execute();
return $query -> fetchAll();
}
On my last question someone told me to start using PDO, so I did :)
My problem is when someone fills in the make field on my form and nothing else it will return nothing. I thought if the get variables were blank I would append a * and it will return anything but this is not the case :( I've been searching but I think my problem is I don't know the correct words to search to find the cure for my problem...
Any help would be greatly appreciated.