动态构建SQL搜索查询?

I am trying to build a search query based on the input from users, but I am having problems with the AND and WHERE keywords. Here is the code:

if (isset($_POST['submitBtn'])) {

    $gender = $_POST['gender'];
    $level = $_POST['level'];
    $status = $_POST['status'];

    $query = 'SELECT * FROM candidate ';
    $where = array();

    $criteria = array('gender' => $gender, 'level' => $level, 'status' => $status);
    foreach ($criteria as $key => $value) {
        if ($value !== 'all') {
            $where[] = $key . ' = ' . $value;
        }
    }

}

The output looks like this:

Array
(
    [0] => gender = masculine
    [1] => level = low
    [2] => status = future
)

If no option is selected, it defaults to 'all' and it is excluded from the $where[].

I need to achieve this, or anything similar:

Array
(
    [0] => WHERE gender = masculine
    [1] => AND level = low
    [2] => AND status = future
)

The WHERE must be appended only if one or more options have been selected and the AND must be appended only if two or more options have been selected.

In the code I am using I have 9 search inputs. To keep it clear I only displayed three in the snippet. Can you please help me figure this out?

Try this:I think you need the whereClause in string not in array,here you can choose any one from two and remove the other one.

<?php 
$where=array();$flag=0;// use flag to identify the where/and
 $whereClause="";
 $criteria = array('gender' => "masculine", 'level' => "low", 'status' => "future");
    foreach ($criteria as $key => $value) {
        if ($value !== 'all') {
            if($flag == 0){
             $where[] = " WHERE " .$key . ' = ' . $value;//if you need array
             $whereClause='WHERE '.$key . ' = "' . $value.'"';//if you need string
            }else{
             $where[] = " AND " .$key . ' = ' . $value;
             $whereClause .=' AND '.$key . ' = "' . $value.'"';
            }
             $flag++;
        }
    }
    echo "<pre>";
    print_r($where);
    echo "</pre>";

   echo $whereClause;
?>

You need to put one incrementer ($inc) and then put the conditions as:

$inc=1;
foreach ($criteria as $key => $value) {
        if ($value !== 'all') {
            if($inc==1){
            $where[] = 'Where '.$key . ' = ' . $value.'';
            }else{
            $where[] = 'AND '.$key . ' = ' . $value.'';
            }

            $inc++;
    }
}

In My view there is one more clean way of achiving this:

if (isset($_POST['submitBtn'])) {

    $gender = $_POST['gender'];
    $level = $_POST['level'];
    $status = $_POST['status'];

    $query = 'SELECT * FROM candidate ';
    $where = array("Where 1=1");

    $criteria = array('gender' => $gender, 'level' => $level, 'status' => $status);
    foreach ($criteria as $key => $value) {
        if ($value !== 'all') {
            $where[] = 'AND '.$key . ' = ' . $value.' ';
        }
    }

}

You can do this :

 $query = 'SELECT * FROM candidate ';
 $where='';
 $criteria = array('gender' => $gender, 'level' => $level, 'status' => $status);
 foreach ($criteria as $key => $value) {
    if ($value !== 'all') {
        if($where=='')
            $where='WHERE '.$key . ' = ' . $value;
        else
            $where.=' AND '.$key . ' = ' . $value;

    }
}
$query.=$where; //final query

You can use simple switch statement too

<?
    if (isset($_POST['submitBtn'])) {

        $gender = $_POST['gender'];
        $level = $_POST['level'];
        $status = $_POST['status'];

        $query = 'SELECT * FROM candidate ';
        $where = array();

        $criteria = array('gender' => $gender, 'level' => $level, 'status' => $status);
        foreach ($criteria as $key => $value)
        {
            if ($value !== 'all')
            {
                switch ($key)
                {
                    case 1:
                    {
                        $query = " WHERE " .$key . ' = ' . $value;
                        break;
                    }

                    case 2:
                    {
                        $query = " AND " .$key . ' = ' . $value;
                        break;
                    }

                    case 3:
                    {
                        $query = " AND " .$key . ' = ' . $value;
                        break;
                    }

                }
                $where[] = $query;
            }
        }

    }