I have this database project due Monday and I am trying to create a PHP script to do a simple search query in the MySQL database. This is what I have...
search.html
<html>
<head>
<title>NOMO Auto Group Inventory Search</title>
</head>
<body>
<h2>NOMO Auto Group Inventory Search</h2>
<form action="results.php" method="post">
Choose Search Type:<br />
<select name="searchtype">
<option value="Make">Make</option>
<option value="Model">Model</option>
<option value="Year">Year</option>
<option value="Color">Color</option>
</select>
<br />
Enter Search Term:<br />
<input name="searchterm" type="text" size="40"/>
<br/>
<input type="submit" name="submit" value="Search"/>
</form>
</body>
</html>
results.php
<html>
<head>
<title>NOMO Auto Group Search Results</title>
</head>
<body>
<h2>NOMO Auto Group Search Results</h2>
<?php
//create short variable name
$searchtype=$_POST['searchtype'];
$searchterm=$_POST['searchterm'];
if(!$searchtype || !$searchterm) {
echo 'You have not entered any search details. Please go back and try again.';
exit;
}
if (!get_magic_quotes_gpc()){
$searchtype = addslashes($searchtype);
$searchterm = addslashes($searchterm);
}
@ $db = new mysqli('(db IP)', '(db username)', '(db pw), 'database');
if (mysqli_connect_errno()) {
echo 'Error: Could not connect to database.';
exit;
}
$query = "select * from Inventory where ".$searchtype." like '%".$searchterm."%'";
$result = $db->query($query);
$num_results = $result->num_rows;
echo "<p>Number of cars found: ".$num_results." </p>";
for ($i=0; i <$num_results; $i++) {
$row = $result->fetch_assoc();
echo "<p><strong>".($i+1).". Make: ";
echo stripslashes($row['Make']);
echo "</strong><br />Model: ";
echo stripslashes($row['Model']);
echo "<br />Color: ";
echo stripslashes($row['Color']);
echo "<br />Year: ";
echo stripslashes($row['Year']);
echo "</p>";
}
$result->free();
$db->close();
?>
</body>
</html>
This is what I get when I search for the Make Acura...
NOMO Auto Group Search Results
query($query); $num_results = $result->num_rows; echo "
Number of cars found: ".$num_results."
"; for ($i=0; i <$num_results; $i++) { $row = $result->fetch_assoc(); echo "
".($i+1).". Make: "; echo stripslashes($row['Make']); echo "
Model: "; echo stripslashes($row['Model']); echo "
Color: "; echo stripslashes($row['Color']); echo "
Year: "; echo stripslashes($row['Year']); echo "
"; } $result->free(); $db->close(); ?>