0

How do i write a code that builds a mysql query depending on what values drop lists have?

If nothing is chosen in a drop list, then the drop list value is 001 so then the query should not include this drop list in the search!

Please help...

I have this so far:

            foreach($_GET as $key => $value) {
    if ($value != '001') {
                 Do something smart...like add to a query...
                     }
        }
4
  • what do you have so far? You haven't given anyone anything to work with... what does your manual query look like? what does the html look like? Commented Oct 13, 2009 at 14:50
  • 1
    also why is this tagged perl? Commented Oct 13, 2009 at 14:52
  • If you explicity set the option's value to nothing <option value="">[Select Something]</option> then nothing will get sent to the server in case that helps. Commented Oct 13, 2009 at 14:55
  • You may consider ORM-like solutions, that give you proper level of abstraction and allow you to achieve what you requested (adding elements of the query) in a very clean way. Commented May 28, 2011 at 22:22

2 Answers 2

1

Send the form to a PHP file called (say) script.php with method GET (or POST, if you prefer - in which case replace the references to GET below):

In script.php include the following:

<?php
if (!isset($_GET['yourdroplistname']) {
  $value = 001;
} else {
  $value = mysql_real_escape_string($_GET['yourdroplistname']);
}
mysql_query("YOUR QUERY, CONTAINING $value WHERE APPROPRIATE");
?>
Sign up to request clarification or add additional context in comments.

1 Comment

Never use direct user input to construct raw SQL query. Use a placeholder variable, escape special characters in the value or cast it to int to make sure it contains a number.
0

I recommend to use a switch($droplist) to filter what PHP should do.

switch($droplist)
case '1':
$query = 'SELECT 1 FROM xy WHERE userid = 1';
break;
case '2':
// etc.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.