The search contains only results for the submitted search input value.
P粉541551230
2023-07-25 09:17:02
<p>Hi, I'd be happy to help you with your search script if possible. </p><p>I have a search function that retrieves data about vehicles from the database using three types of search inputs and a submit button. </p><p>Input 1: option list search (searcha)</p><p>Input 2: text search (searchb)</p><p>Input 3: two single Select button to select search (you can select one or two buttons)</p><p>There is also a submit button. </p><p>When I submit a search, the following PHP code runs. It will display the results, but only if they match the first correct result in the database. I want it to be able to search results that only contain the submitted search input value. </p><p>For example, if I select an option from input 1 and a radio button from input 3, the search should only find rows containing both values (if that makes sense). </p><p><br /></p>
<pre class="brush:php;toolbar:false;"><?
//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['radio1'], $_POST['radio2'])) {
$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['radio1'])) {
$stmt = $pdo->prepare('SELECT * FROM coun WHERE ref = ?');
$stmt->execute([ 'In' ]);
$search_results = $stmt->fetchAll(PDO::FETCH_ASSOC);
} else if (isset($_POST['radio2'])) {
$stmt = $pdo->prepare('SELECT * FROM coun WHERE ref1 = ?');
$stmt->execute([ 'On' ]);
$search_results = $stmt->fetchAll(PDO::FETCH_ASSOC);
} else {
$error = 'No options selected!';
}
?></pre>
<p>Is there an easy way to adjust this script to reflect the entered search? I'm just starting to learn PHP, so a proper guide would be perfect. Thanks. </p><p><br /></p>
First of all, I don't understand how to select two radio buttons at the same time, do you mean checkboxes? Regarding your question - anyway, you should execute a query, but build it based on your $_POST value
maybe like this: