I am trying to do an advanced search, i had this working but i was making full queries for each get variable, which was not good but it worked
now i am trying to build the query depending on if the url variable are empty. But i cant figure our why this is not working. The query works as i have tested this, am i not building the query correctly?
this is the function
public function search($man, $mod, $min, $max)
{
$this->currentLocation();
$bind = '';
$query .= 'SELECT listed_watches.*, watch.*, watch_images.* FROM listed_watches';
$query .= 'LEFT JOIN watch ON watch.Id = listed_watches.watchID';
$query .= 'LEFT JOIN watch_images ON listed_watches.url = watch_images.url';
$query .= 'WHERE listed_watches.sellto REGEXP :search';
if(!empty($man)){
$query .= 'AND listed_watches.manufacture = :man';
$bind .= "$smt->bindValue(':man', $man);";
}
if(!empty($mod)){
$query .= 'AND listed_watches.model = :mod';
$bind .= "$smt->bindValue(':mod', $mod);";
}
if(!empty($min)){
$query .= 'AND listed_watches.minPrice > :min';
$bind .= "$smt->bindValue(':min', $min);";
}
if(!empty($max)){
$query .= 'AND listed_watches.maxPrice < :max';
$bind .= "$smt->bindValue(':max', $max);";
}
$query .= 'GROUP BY listed_watches.id';
$smt = $this->pdo->prepare(''.$query.'');
$smt->bindValue(':search', "^[".$this->userLocation."]");
$bind;
return $smt->fetchAll();
}
And when i search and enter choose a $man and $mon i get this error
Undefined variable: smt
Trying to get property of non-object in
this error is refereeing to this line
$bind .= "$smt->bindValue(':mod', $mod);";
First you need create $smt object, then you can use method on it.
if(!empty($man)){
$query .= 'AND listed_watches.manufacture = :man';
}
if(!empty($mod)){
$query .= 'AND listed_watches.model = :mod';
}
// ...
$query .= 'GROUP BY listed_watches.id';
$smt = $this->pdo->prepare(''.$query.'');
$smt->bindValue(':search', "^[".$this->userLocation."]");
if(!empty($man)){
$smt->bindValue(':man', $man);
}
if(!empty($mod)){
$smt->bindValue(':mod', $mod);
}