473,385 Members | 1,317 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Run a SQL query based on HTML drop down selection?

Joe
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";

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?
Oct 30 '08 #1
4 11867
Joe wrote:
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";

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?
There should be no problem - as long as your columns are named Title,
Year, Genre and Director.

What do you get when you echo $query, and what is your error message?

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Oct 30 '08 #2
Jerry Stuckle wrote:
Joe wrote:
>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";

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?

There should be no problem - as long as your columns are named Title,
Year, Genre and Director.

What do you get when you echo $query, and what is your error message?
A couple of possibilities to try:

- does $searchby need to be in quotes? ie .... GROUP BY '$searchby'"
- depending on your php setup, you may need to use $_GET['searchby']
rather than $searchby - although this would affect your other form
parameters too.
Oct 30 '08 #3
On 30 Oct, 07:34, GazK <inva...@invalid.invalidwrote:
Jerry Stuckle wrote:
Joe wrote:
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";
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?
There should be no problem - as long as your columns are named Title,
Year, Genre and Director.
What do you get when you echo $query, and what is your error message?

A couple of possibilities to try:

- does $searchby need to be in quotes? ie .... GROUP BY '$searchby'"
No. Its a column name.
- depending on your php setup, you may need to use $_GET['searchby']
rather than $searchby - although this would affect your other form
parameters too.
No - register_globals is deprecated - even if it is configured as 'on'
you should write your code as if it were off.

And, even though MySQL only allows one query per function call, there
should be validation in place to prevent SQL injection.

OP's code should work.

C.
Oct 30 '08 #4
On Oct 29, 8:55 pm, Joe <sutclif...@gmail.comwrote:
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";

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?
The first thing I notice is that if You search by Year, the column
name will not agree with the db table. A troubleshooting technique i
use is to build the query in a variable, then echo out the
variable(the query) to make sure the right query is being run.
Hope this helps....
Nov 7 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Steve Speirs | last post by:
Hi I'm trying to show/hide a simple piece of text and a text field on a form based on what choice is made from a drop down box. <select name="dropdown" size="1"> <option selected...
1
by: Gx | last post by:
I have this form with the fields: * Product: (drop down combo box) with choices - X, Y * Amount: (currency ) * Balance (currency) I want to associate product X with the percentage...
3
by: Mike | last post by:
I have a C# web app were i want to run a SQL query based on the selection from the drop down. I have 4 selections on the drop down and I can pass 3 of them fine due to they are in the database. I...
1
by: Ed244 | last post by:
Hi, I'm in the process of producing a database for a college project and have a slight problem I was wondering whether you could help me with. The area I'm having problems with takes a value...
2
by: ponyeyes | last post by:
Hi There, I am a bit of a newbie to PHP programming and I would like to know how I can place a selected drop down option into a PHP variable and then produce an sql query which incorporates this...
6
by: nbomike | last post by:
I am trying to integrate a custom page into a shopping cart app written in PHP. The custom page is basically a part finder widget that helps customers find a car part based on the car's make, model,...
26
by: kpouya | last post by:
I am going to be as clear as possible about what i got right now and what i want to achieve Table1: Libary Title - memo Last Name - memo First Name - memo Company - memo Year - number Type -...
0
by: tkip | last post by:
Gents.. I am just looking for ideas as to how to approach the query of DB I am working on. Basically, it's a database of welding procedures and depending on the metal you choose, appropriate...
21
by: scaleautostyle | last post by:
hi everybody... i ran across the site and found TONS of information about what i want to do.... but i don't know what to take for my need. here is what i want to do... a search engine for my...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.