Jump to content

Let's say I have the following HTML dropdown.

<form action="filter.php" method="post" name="filter_dropdown"><select name="categories_filter"><option value="select" selected="selected">Select Search Field</option><option value="news">News</option><option value="recipes">Recipes</option><option value="funny">Funny</option></select>
 

I'd like to run a MYSQL query based on what the user selected from the dropdown, e.g. if News is selected I want a list of all bookmarks that are categorized as news to be displayed in a table. How would I do this?

Link to comment
https://linustechtips.com/topic/378180-mysql-query-based-on-dropdown/
Share on other sites

Link to post
Share on other sites


if($_POST['categories_filter'] == "news")

{

//news stuff

} else if($_POST['categories_filter'] == "recipes")

{

//recipes stuff

}else if($_POST['categories_filter'] == "funny")

{

//funny stuff

}else

{

//error with form.

}

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to post
Share on other sites

Just something like "select from bookmarks where category = ?".

 

Of course, the actual query depends on your database structure. I wouldn't advise actually using this query as you should be using a join table at the minimum for something like this - gotta have that normalization.

 

And you should obviously be using PDO and prepared statements.

Link to post
Share on other sites

Just something like "select from bookmarks where category = ?".

 

Of course, the actual query depends on your database structure. I wouldn't advise actually using this query as you should be using a join table at the minimum for something like this - gotta have that normalization.

 

And you should obviously be using PDO and prepared statements.

 

Just gonna quote this because it needs to be repeated over and over and over.

--Neil Hanlon

Operations Engineer

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×