Hi there looking for a little help if possible with a search script.
I have a search function that retrieves data from a database about vehicles using three types of search input with one submit button.
Input 1. An options list search (searcha)
Input 2. Text search (searchb)
Input 3. 2 Radio buttons selection search (either button selected or both)
Plus a submit button
I have the below PHP code which runs when I submit the search. It does show the result but only it matches the first correct result from the database then shows that. What I would like it to do is to search for the results that only hold the submitted search inputs values.
For example, if I say chose an option from Input 1 & a radio button from Input 3 the search would find only row(s) that hold both those values if that makes sense.
<?
// Vehicles
if (in_array($_POST['searcha'], $search_a_option_list)) {
$stmt = $pdo->prepare('SELECT * FROM coun WHERE inth = ?');
$stmt->execute([ $_POST['searcha'] ]);
$search_results = $stmt->fetchAll(PDO::FETCH_ASSOC);
} else if ($_POST['searchb']) {
$stmt = $pdo->prepare('SELECT * FROM coun WHERE bunty LIKE ?');
$stmt->execute([ '%' . $_POST['searchb'] . '%' ]);
$search_results = $stmt->fetchAll(PDO::FETCH_ASSOC);
} else if (isset($_POST['check1'], $_POST['check2'])) {
$stmt = $pdo->prepare('SELECT * FROM coun WHERE ref = ? OR ref1 = ?');
$stmt->execute([ 'In', 'On' ]);
$search_results = $stmt->fetchAll(PDO::FETCH_ASSOC);
} else if (isset($_POST['check1'])) {
$stmt = $pdo->prepare('SELECT * FROM coun WHERE ref = ?');
$stmt->execute([ 'In' ]);
$search_results = $stmt->fetchAll(PDO::FETCH_ASSOC);
} else if (isset($_POST['check2'])) {
$stmt = $pdo->prepare('SELECT * FROM coun WHERE ref1 = ?');
$stmt->execute([ 'On' ]);
$search_results = $stmt->fetchAll(PDO::FETCH_ASSOC);
} else {
$error = 'No options selected!';
}
?>
Is there a simple way to adjust this script to reflect the inputted searches? Just starting to learn PHP so a little nudge in the right direction would be perfect.
Regards