Re: Run a SQL query based on HTML drop down selection?



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
.



Relevant Pages

  • Re: Run a SQL query based on HTML drop down selection?
    ... And here is the relevant PHP query: ... Assume that "$title" is a string of text (not necessarily a movie ... My problem seems to be that I cant use the variable $searchby ... Year, Genre and Director. ...
    (comp.lang.php)
  • Run a SQL query based on HTML drop down selection?
    ... And here is the relevant PHP query: ... Genre ... Assume that "$title" is a string of text (not necessarily a movie ... My problem seems to be that I cant use the variable $searchby ...
    (comp.lang.php)
  • Re: Run a SQL query based on HTML drop down selection?
    ... And here is the relevant PHP query: ... Assume that "$title" is a string of text (not necessarily a movie ... My problem seems to be that I cant use the variable $searchby ... Year, Genre and Director. ...
    (comp.lang.php)
  • Re: Run a SQL query based on HTML drop down selection?
    ... And here is the relevant PHP query: ... Genre ... Assume that "$title" is a string of text (not necessarily a movie ... My problem seems to be that I cant use the variable $searchby ...
    (comp.lang.php)
  • Re: Creatinig a database to update in alphabetical order
    ... You can then create a Query which takes all of the records in the ... Some attributes of a movie would be the Title, the studio, ... MovieID Autonumber Primary Key ' links tables together ... Keyword ...
    (microsoft.public.access.gettingstarted)