user188971
user188971

Reputation:

automatic mysql query in PHP

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

Upvotes: 0

Views: 861

Answers (2)

tog22
tog22

Reputation: 526

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");
?>

Upvotes: 1

Julius F
Julius F

Reputation: 3444

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.

Upvotes: 0

Related Questions