www.webdeveloper.com
Results 1 to 5 of 5

Thread: Show all records if nothing selected from drop down

  1. #1
    Join Date
    Jun 2008
    Location
    London
    Posts
    175

    Show all records if nothing selected from drop down

    Hi All,

    Kinda new to this. I've got a MySQL search query working using 2 drop downs, so if white and mermaid or ivory and fishtail is selected, the results are returned successfully. I can't figure out how to get every color/style to display if nothing is selected in one of the dropdowns. So just selecting 'white' should return all white fishtails and mermaids. Any help would be really appreciated I've put a demo here to show what I mean http://goo.gl/E4vzuM

    PHP Code:
    // Snippet from my PHP...

    if (isset($_REQUEST['submit']))
    {    
    // Show results from dropdowns
        
    $drop1 $_POST['search1'];
        
    $drop2 $_POST['search2'];
        
    $query_all "SELECT * FROM products WHERE `colour` LIKE '%".$drop1."%' AND `style` LIKE '%".$drop2."%'";    
    }
    else 
    // Show all
    {
        
    $query_all "SELECT * FROM products";

    HTML Code:
    <select name="search1" id="search1">
        <option value="" selected="selected">Please Select</option>
        <option value="white">white</option>
        <option value="ivory">ivory</option>
     </select>
      
     <select name="search2" id="search2">
         <option value="" selected="selected">Please Select</option>
        <option value="fishtail">fishtail</option>
        <option value="mermaid">mermaid</option>
     </select>
      
      <input name="submit" type="submit" value="Submit"/>

  2. #2
    Join Date
    Dec 2011
    Location
    Centurion, South Africa
    Posts
    784
    Untested, but what about:

    PHP Code:
    $query_all 'SELECT * FROM products WHERE 1=1' . ($drop1 ' AND `colour` LIKE "%' $drop1 '%"' '') . ($drop2 ' AND `style` LIKE "%' $drop2 '%"' ''); 
    Don't forget to escape your user input to prevent SQL injections.
    JavaScript: Learn | Validate | Compact

  3. #3
    Join Date
    Jun 2008
    Location
    London
    Posts
    175
    Awesome, thank you. Not sure what escaping is but I'll have a read up on it. Thanks again

  4. #4
    Join Date
    Jun 2008
    Location
    London
    Posts
    175
    didn't think that was needed with drop downs but looks like some tools can allow user input. Thanks for the heads up

  5. #5
    Join Date
    Dec 2011
    Location
    Centurion, South Africa
    Posts
    784
    Don't trust your data, always sanitise.

    Use mysql_real_escape_string if you haven't already discovered it.
    JavaScript: Learn | Validate | Compact

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles