I have a drop down menu that I use to you choose from and this results in a value given to the next page. I would like to use this value to determine where to select from. So the possible values are car and toys. So I would like to let the user use the drop down menu to choose car which results in the database selecting from car to find the item.. If the user chooses toys I would have them select from toys in order to find the item. When I attempt to use this code I just get an error. If I delete '%item%' the error is gone. Any help would be loved. Thanks.
Submitting
<form action="search.php" method="post">
<select>
<option value>Choose A Item</option>
<option value="car" name="item">Car</option>
<option value="toys" name="item">Toys</option>
</select>
<input type="submit" name="submit" value="Submit" />
</form>
Searching
$term = $_POST['term'];
$item = $_POST['item'];
$query = mysql_query("select * from '%$item%' where Items like '%$term%'");
if(mysql_num_rows($query) <= 0)
{
echo "<center>No results. Please try another item.</center>";
} else {
while ($row = mysql_fetch_array($query)) {
echo "<p2>";
echo $row['Items'], ' - Location ' .$row['Loc'];
echo '<br/><br/>';
echo "</p2>";
}
}
?>
I think your query should be changed into: $query = mysql_query("select * from '$item' where Items like '%$term%'");
1.) - don't use mysql_query - its an outdated library and your query as written above is subject to SQL injection - the most common compromise on the internet.
2.) don't use like '%...%' as this can't be indexed and will be horribly slow on any medium size table or greater. try to at least remove the first % to allow indexing.