Try creating your input with names that php will create arrays from, then
you can simply join the array elements if they are non-empty with something
like " OR $key='$val'".

An example:

  <input type="text" name="filter[haircolor]">
  <input type="checkbox" name="filter[registered]" value='Y'>

When submitted you could then:
  $clause = '';
  if (sizeof($_POST['filter']) > 0) {
    $clause = 'WHERE ';
    foreach($_POST['filter'] as $key=>$val) {
        $tmp[] = "$key LIKE '%$val%'";
    $clause .= join(' OR ', $tmp);
  $sql = 'SELECT * FROM table $clause';

Of course you may want to add checks on what was entered or build a
mechanism so that you don't necessarily join all the filters with 'OR'. But,
I think you get the idea.  Hope it helps.


> -----Original Message-----
> From: Geoffrey Makstutis [mailto:[EMAIL PROTECTED]]
> Sent: Tuesday, March 05, 2002 8:12 AM
> Subject: [PHP-DB] multiple select statements
> Hi,
> I've got an HTML form which allows users to select various 
> criteria to search for in my database (MySQL). 
> The problem is that I can't seem to figure out how create the 
> SELECT statement, given the fact that they could choose any 
> or none of the criteria. 
> Does anyone know of a way to dynamically create a SELECT 
> statement that might have different numbers of criteria?
> Thanks

PHP Database Mailing List (
To unsubscribe, visit:

Reply via email to