User filtered results from php MySQL database
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 开发者_JAVA技巧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
May be you have to get your $searchVar from the super globals array $_GET:
<? $searchVar = $_GET['searchVar'] ?>
PHP does not read variables directly from the querystring since it is a matter of security.
You need to use Javascript and AJAX to send the information back to the server.
PHP is executed on the server, and PHP renders the HTML and sends that HTML to the client. The PHP script is then done executing. Once the client receives the HTML, the browser renders the HTML and displays the results to the user. The user interacts with the components of the HTML (such as dropdowns). The point is, despite the way it looks, PHP executes COMPLETELY before any of the HTML executes. So interaction between the client and the HTML is not as easy as you might initially think.
That said, this is a common problem that many people have encountered and solved; the solution is in the form of something called AJAX, wherein Javascript that runs on the client in HTML sends dynamic requests to the server, which can be executed in PHP to retrieve data, which is returned to the client Javascript, which can then format the data and modify the page appropriately.
There's another way to do this that's a bit simpler, but that has a slightly less pleasant user experience; you can have the HTML page do a POST back to the server with the data that you want to be selected on the server, and then the PHP can read the POST variable and make the query from there. It's simpler, but the problem is that the user experience is less pleasant, because the entire page needs to be refreshed.
Your <option>
tags almost certainly have the wrong values, and your form isn't constructed properly:
<form action="mysearch.php" method="post">
<select name="jumpMenu">
<option value="Miami">Miami</option>
<option value="Miami Beach">Miami beach</option>
</select>
</form>
Your form, as constructed in your sample, would be trying to do:
SELECT ... WHERE loding.city = 'directory.php?searchVar=Miami'
where you should be doing
SELECT ... WHERE lodging.city = 'Miami'
精彩评论