Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7587303
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T19:38:24+00:00 2026-05-30T19:38:24+00:00

I am currently creating a multiple dropdown query where the user can query by

  • 0

I am currently creating a multiple dropdown query where the user can query by (3) factors for returning results, my issue is how can I do this in an efficient manner so I am not obscurely writing multiple possible MySQL Queries.

<select name="class"> 
<OPTION VALUE='any'>Choose Class
<option value="a">Block A</option>
<option value="b">Block B</option>
<option value="c">Block C</option>
</select>

 <select name="category"> 
 <OPTION VALUE='any'>Choose Type
 <option value="math">Math</option>
 <option value="science">Science</option>
 <option value="history">History</option>
 </select>

How can I successfully create a MySQL query that will correctly select the right parameters in the case that a variable is missing. In example, if they choose to do the first dropdown and only search for the “class” and not choose the second dropdown. I want to be able to do the first query, the second query or both of them. I have the PHP, Ajax written, I’m just stumped as to the correct structure of the MySQL query.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T19:38:25+00:00Added an answer on May 30, 2026 at 7:38 pm

    Put your “WHERE” clause conditions in one array. I would do like this:

    // filter out invalid values is important
    $valid_class = array('a', 'b', 'c');
    $valid_category = array('math', 'science', 'history');
    
    // initialize array for WHERE clause conditions
    $where = array('TRUE');
    
    if (in_array($_POST['class'], $valid_class))
    {
        $where[] = 'class = "' . $_POST['class'] . '"';
    }
    
    if (in_array($_POST['category'], $valid_category))
    {
        $where[] = 'category = "' . $_POST['category'] . '"';
    }
    
    // use the array with the "implode" function to join its parts
    $sql = 'SELECT * FROM table WHERE ' . implode(' AND ', $where);
    

    You may want to initialize the $where array with something more interesting than “TRUE” (which is there in case the user does not filter by class neither category, because we don’t want an empty $where array reaching the last line). For example:

    $where = array();
    $where[] = 'name = "' . mysql_real_escape_string($_POST['name']) . '"';
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently creating an explicit reference to this in the outer class so that
I am currently creating a type of classifieds website, and when the user enters
I'm currently creating a user interface using WPF and would like to display a
I am creating an application which uses a vCard struct . Currently, this struct
I'm currently creating a stock management system that uses multiple warehouses (with sub locations)
I'm currently working on creating a private messaging system, (PHP/MySQL) in which users can
I'm creating a card game with multiple classes. Currently, I'm using global variables to
I’m currently creating an iOS app that uploads files to a server. As multiple
I'm creating an application that needs to run under multiple databases. I currently have
I currently have some code which needs to perform multiple updates per user for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.