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

  • SEARCH
  • Home
  • 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 7712583
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T01:40:05+00:00 2026-06-01T01:40:05+00:00

I’m trying to create an Advanced Searching form that sort of look like this

  • 0

I’m trying to create an Advanced Searching form that sort of look like this ;

http://img805.imageshack.us/img805/7162/30989114.jpg

but what should I write for the query?

I know how to do it if there is only two text box but three, there’s too many probability that user will do.

$query = "SELECT * FROM server WHERE ???";

What should I write for the “???”

I know how to use AND OR in the query but lets say if the user only fill two of the textbox and one empty. If I write something like this ;

$query = "SELECT * FROM server WHERE model='".$model."' and brand='".$brand."' and   SN='".$SN.'" ";

The result will return as empty set. I want the user can choose whether to fill one,two or three of the criteria. If I use OR, the result will not be accurate because if Model have two data with the same name (For example :M4000) but different brand (For example : IBM and SUN). If I use OR and the user wants to search M4000 and SUN, it will display both of the M4000. That’s why it is not accurate.

  • 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-06-01T01:40:06+00:00Added an answer on June 1, 2026 at 1:40 am

    If the user can decide how many criteria he wants to enter for your search and you want to combine those criteria (only those actually filled by the user), then you must dynamically create your SQL query to include only those fields in the search that are filled by the user. I’ll give you an example.

    The code for a simple search form could look like this:

    $search_fields = Array(
        // field name => label
       'model'     => 'Model',
       'serialNum' => 'Serial Number',
       'brand'     => 'Brand Name'
    );
    echo "<form method=\"POST\">";
    foreach ($search_fields as $field => $label) {
        echo "$label: <input name=\"search[$field]\"><br>";
    }
    echo "<input type=\"submit\">";
    echo "</form>";
    

    And the code for an actual search like this:

    if (isset($_POST['search']) && is_array($_POST['search'])) {
        // escape against SQL injection
        $search = array_filter($_POST['search'], 'mysql_real_escape_string');
        // build SQL
        $search_parts = array();
        foreach ($search as $field => $value) {
            if ($value) {
                $search_parts[] = "$field LIKE '%$value%'";
            }
        }
        $sql = "SELECT * FROM table WHERE " . implode(' AND ', $search_parts);
        // do query here
    }
    else {
        echo "please enter some search criteria!";
    }
    

    In the above code we dynamically build the SQL string to do a search (“AND”) for only the criteria entered.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
Basically, what I'm trying to create is a page of div tags, each has
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I am trying to render a haml file in a javascript response like so:
I know there's a lot of other questions out there that deal with this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm not entirely sure how I managed to jack this up. http://pretty-senshi.com If you

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.