I am doing a database search using php. Everything works rights except when I just press the enter button (not searching anything) or a match is not found. In those two situations, I want a message displaying no items were found. Currently, when I search for nothing by pressing submit, all the entries show up. When I search for something that isn't there, the table headers display. How can I fix this?
Also, this is a video game site and I would like links to display what is for sale for each system (Xbox, Wii, Playstation, etc). I know I could do seperate pages and have a select statement that selects everything with that system Id but would there be a more efficient way to create the page instead of having 24 different pages?
Below is the code for searching:
<link rel="stylesheet" type="text/css" href="styles.css" />
<?php # search_games.php
// This script searches the available selections for a given product name.
// Set the page title and include the HTML header.
$page_title = 'Search the Video Game Database';
$name = $_POST['select_name'];
$query = "SELECT productName, price, image_name, systemName, genre
FROM products, systems, genre
WHERE productName LIKE '%$name%'
AND systems.systemid = products.systemid
AND genre.genreid = products.genreid";
// Set up the database connection.
require_once ('mysqli_connect.php');
// Run the query and retrieve the result
$result = mysqli_query ($dbc, $query);
// If the result is not empty
if ($result <> '') {
// Create the table head.
echo '<center>Click on a selection below to add to your shopping cart.</center>';
echo '<table border="1" width="90%" cellspacing="3" cellpadding="3" align="center">
<tr>
<td align="middle" width="20%"><b>Video Game Name</b></td>
<td align="middle" width="30%"><b>Price</b></td>
<td align="middle" width="30%"><b>System</b></td>
<td align="middle" width="30%"><b>Genre</b></td>
<td align="middle" width="30%"><b>Image</b></td>
</tr>';
while ($row = mysqli_fetch_array ($result, MYSQLI_ASSOC)) {
// Display each record.
echo '<tr>
<td align="middle">' . $row['productName'] . '</td>
<td align="middle">' . $row['price'] . '</td>
<td align="middle">' . $row['systemName'] . '</td>
<td align="middle">' . $row['genre'] . '</td>
<td align="middle"><IMG SRC=images\/' . $row['image_name'] . '></td>
</tr>';
} // End of while loop.
// Close the table.
echo '</table>';
// Free up the resources.
mysqli_free_result($result);
}
else {
echo 'No result matching the search criterion.<br>';
}
echo '<p><center><a href="query_games.html">Search Again</a></center><p>';
mysqli_close($dbc); // Close the database connection.
?>