PHP/MySQL newbie here.
I managed to develop a web page that displays info from my database by following an online tutorial. Now, I just want to add a way for users to filter that information. I understand how it can be filtered using WHERE, but I cannot figure out how to let the user update that info.
Here’s what I tried:
$query_rsLodging = "SELECT name, address, city FROM lodging WHERE lodging.city = '". $searchVar . "' ORDER BY lodging.name";
Then I tried passing info to the variable using a jumpmenu:
<form name="form" id="form">
<select name="jumpMenu" id="jumpMenu" onChange="MM_jumpMenu('parent',this,0)">
<option value="#">Sort by...</option>
<option value="directory.php?searchVar=Miami">Miami</option>
<option value="directory.php?searchVar=Miami Beach">Miami Beach</option>
</select></form>
The page reloads with the appended URL, but it does not affect the results. However, placing the following code just above the query does:
$searchVar = "Miami"
Obviously, I’m not passing the info to the variable correctly, but I feel like I’m close.
Question: What is the best way to let my users filter the results of my database (preferably via dropdown menu)?
To get query string variables into PHP scope use the $_GET and $_POST.
In your case, where you putted the
$searchVar = "Miami"replace it with$searchVar = $_GET['searchVar'].As I wrote before, Boby tables will eat your DB. Read here how to avoid sql injection