Re: Run a SQL query based on HTML drop down selection?
- From: Charles Calvert <cbciv@xxxxxxxxx>
- Date: Thu, 30 Oct 2008 23:10:13 -0400
On Wed, 29 Oct 2008 18:55:11 -0700 (PDT), Joe <sutcliffe1@xxxxxxxxx>
wrote in
<0041d01e-a9d2-4a0e-aba9-c8d03682868e@xxxxxxxxxxxxxxxxxxxxxxxxxxxx>:
This is my HTML form:
<form method=get action="home.php">
<INPUT TYPE = "Text" VALUE = "" NAME = "title"><br>
<select name="searchby">
<option value="Title">Title</option>
<option value="Year">Year</option>
<option value="Genre">Genre</option>
<option value="Director">Director</option>
</select>
And here is the relevant PHP query:
$query = "SELECT Title, ReleaseYear, Rating, Director, RunningTime,
Genre
FROM movie
WHERE $searchby LIKE '%$title%'
GROUP BY $searchby";
FYI, "GROUP BY" is not the same thing as "ORDER BY", though you may
get similar results depending on several variables. If you're trying
to sort the results, you should use "ORDER BY".
Assume that "$title" is a string of text (not necessarily a movie
title). My problem seems to be that I cant use the variable $searchby
within a SQL query. Is there a workaround for this?
Assuming, as Jerry said, that the values of your options exactly match
the names of your columns (including case for some RDBMSes), it should
be fine. There's no reason that you can't use $searchby in your query
(though you should verify that it's one of the allowed values to avoid
SQL injection).
--
Charles Calvert | Web-site Design/Development
Celtic Wolf, Inc. | Software Design/Development
http://www.celticwolf.com/ | Data Conversion
(703) 580-0210 | Project Management
.
- References:
- Prev by Date: Re: Does PHP do this differently?
- Next by Date: Re: PHP mail
- Previous by thread: Re: Run a SQL query based on HTML drop down selection?
- Next by thread: A question about UDP packet receive
- Index(es):
Relevant Pages
|