/    Sign up×
Community /Pin to ProfileBookmark

Multiple SQL queries using a drop-down menu

This really could go in three different sections – XHTML, PHP, and SQL. I chose this section because I need help with the PHP aspect of the code rather than the actual SQL query or drop down menu.

What I have is a drop down menu that will contain five different options:

[code=html]<select>
<option value =”option1″ selected=”selected”>option1</option>
<option value =”option2″>option2</option>
<option value =”option3″>option3</option>
<option value =”option4″>option4</option>
<option value =”option5″>option5</option>
</select>[/code]

I also have a textbox which the user will use to type in a query and search for a person from a database. Now, each person that is in the database has a category beside their name. We can call these categories as option1, option2, option3, option4, or option5. In other words I have a column in my database which contains a category and then another column which contains a person’s name.

What I’d like to be able to do is to allow the user to pick which category they would like to search through before actually clicking the search button. They would pick through a drop-down menu that’s been created by the above code using XHTML.

What would the PHP code for something like that look like?

I would imagine that the best way to tackle this would be by using an IF statement in a sense that if option1 was chosen then perform sqlQuery1 and if option2 was selected then perform sqlQuery2 and so on for all five of them; where each query would do a search through only the category that was selected using the drop-down menu.

I hope that’s enough information for someone to give me a few pointers.

Thank you in advance!

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@Stephen_PhilbinAug 09.2005 — Looks like you could do with some help with yer html as much as yer PHP. Form elements need to be marked up carefully and properly. Labels for the items is great for accessibility etc and there's also the absolute basics you have to learn of each form item too.

In the case of a select box, you must give it at least a name.[code=html]<select name="my_select_box">[/code] So that on the next page, you can access the value in the POST array by its name value. The value indicated in the value attribute of the option item selected at the time of submission with be given to the POST array item with the same name as the select item name attribute value.

So, if the name attribute value was "my_select_box" and the option selected at the time of submission had a value attribute with the value of whichever database table column name you wanted to select from with that option for example "column_3", then on the next page there would be a POST array item available that had the name of "my select_box" and the value of $_POST['my_select_box'] would be "column_3".

So, you could run a MySQL query like:[code=php]mysql_query("SELECT * FROM $_POST[my_select_box]WHERE username = 'the_users_name'");[/code]
×

Success!

Help @00110111 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...