Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Mysql query problem!

Options
  • 22-09-2006 8:17pm
    #1
    Closed Accounts Posts: 8,866 ✭✭✭


    Hey guys, got a problem here, I don't know how to deal with this situation. Its a select query based on a form completed be a user, with 6 variables, only I dont know how to allow for only one or two of the criteria fields to be completed

    In simple terms there are 6 drop down boxes, each makes the search more specific, how do I write a query to allow for all or any of the boxes to be specified.

    Any help going? Cheers!


Comments

  • Registered Users Posts: 3,548 ✭✭✭Draupnir


    Cant you use AND or OR conditions in the SQL statement, then in your code, test whether or not the user has specified one of the conditions, only if the user has specified a condition should you append the AND or OR condition to the SQL before running it.


  • Registered Users Posts: 7,314 ✭✭✭Nietzschean


    if ($_POST["optionalvar"] <> "")
    $SQL .= "AND optionalvar LIKE '%".$_POST["optionalvar"]."%'";
    etc...


  • Registered Users Posts: 4,188 ✭✭✭pH


    Have a look here

    bonkey provides a solution for sqlserver, I think it's possible to user multiple AND with nested ORs checking for blank entries, but I would go with Nietzschean's dynamic approach every time.


  • Closed Accounts Posts: 8,866 ✭✭✭Adam


    if ($_POST["optionalvar"] <> "")
    $SQL .= "AND optionalvar LIKE '%".$_POST["optionalvar"]."%'";
    etc...
    Cheers for that Nietzschean, worked nicely!


Advertisement