I currently have a HTML search form which takes the users input (for example 123456) and using PHP searches a database to see if that number exists as an item number. It then returns information on that item in a table.
Is it possible to search for multiple items at once for example 123456, 654321, 000000 and have the results for each displayed in a table ? I currently have not been able to find any documentation on how I could achieve this. Any help would be greatly appreciated.
My current code which searches and brings back the data for one item is.
<div id="div1">
<!-- [SEARCH FORM] -->
<form method="post" action="nweb.php">
<h1>Product Information</h1>
<input type="text" name="search" required/>
<input type="submit" value="Search"/>
</form>
<?php
if (isset($_POST['search'])) {
require "2-search.php";
if (count($results) > 0) {
foreach ($results as $r) {
echo "<table>";
echo "<tr><td>Item number</td><td>" . $r['item_number'] . "</td></tr>";
echo "<tr><td>Stock available</td><td>" . $r['stock_available'] . "</td></tr>";
echo "<tr><td>Available Stock</td><td>" . $r['available_stock'] . "</td></tr>";
echo "<tr><td>Detailed Description</td><td>" . $r['detailed_desc'] . "</td></tr>";
echo "<tr><td>Gender</td><td>" . $r['gender'] . "</td></tr>";
echo "<tr><td>Group</td><td>" . $r['group'] . "</td></tr>";
echo "<tr><td>Subgroup</td><td>" . $r['sub_group'] . "</td></tr>";
}
echo "</table>";
} else {
echo "No results found";
}
}
?>
</div>
My search code is.
try {
$pdo = new PDO(
"sqlsrv:Server=$server;Database=$database", $username, $password);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
} catch(PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
$stmt = $pdo->prepare ("SELECT * FROM dbo.[data] WHERE [item_number] LIKE ? OR [stock_available] LIKE ?");
$stmt->execute(["%" . $_POST['search'] . "%", "%" . $_POST['search'] . "%"]);
$results = $stmt->fetchAll();
if (isset($_POST['ajax'])) { echo json_encode($results); }
?>