tags:

views:

62

answers:

2

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...
                     }
  }
+1  A: 

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 = $_GET['yourdroplistname'];
}
mysql_query("YOUR QUERY, CONTAINING $value WHERE APPROPRIATE");
?>
tog22
+1 it's a good start
Jonathan Fingland
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.
Lukáš Lalinský
A: 

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.
daemonfire300