I'm fairly new to PHP and database management, but I'm picking it up pretty quickly. I'm trying to create a search bar, I have the searchbar working it just doesn't quite return the results I would like. Essentially, I want to look for a film in my database based on its title (name), subtitle and release date. Here's the code I currently have:
if(isset($_POST['search'])){
$resultArray = '';
$searchTerm = $_POST['searchTerm'];
if($getSearchResults = $conn->query("SELECT type, name, subtitle, release_date, poster_url, slug FROM media WHERE (name OR subtitle OR release_date) LIKE '%$searchTerm%' LIMIT 5")){
$resultArray = array();
while($data = $getSearchResults->fetch_assoc()){
$resultArray[] = $data;
}
}else{
echo 'No Data';
}
echo json_encode($resultArray);
}
This (likely obviously) doesn't work. If the field is empty the returned search results are all the films without a subtitle because that field is empty. However, if I type in a film name, like Avengers, or a release date, like 2018, nothing comes up. It literally only works for the blank fields for some reason?
Ultimately I'd just like your help getting this to return results LIKE the search term, I can live with it searching for blank fields, but if you have a solution for that too then - great!
Thanks