Php 帮助我根据输入构建查询的逻辑

Php 帮助我根据输入构建查询的逻辑,php,Php,我有4个输入字段,这让我抓狂,因为我不知道何时动态添加和条件 现在,我的原始查询如下所示: SELECT * FROM `staff` " . $where . $location . " " . $and_1 . $expertise . " " . $and_2 . $education . " " . $and_3 . $salary . " ORDER BY `created_on` DESC LIMIT " . $limit; 因为我想允许空白字段向查询中添加较少的条件,所以我一直

我有4个输入字段,这让我抓狂,因为我不知道何时动态添加和条件

现在,我的原始查询如下所示:

SELECT *
FROM `staff`
" . $where . $location . "
" . $and_1 . $expertise . "
" . $and_2 . $education . "
" . $and_3 . $salary . "
ORDER BY 
`created_on` DESC LIMIT " . $limit;
因为我想允许空白字段向查询中添加较少的条件,所以我一直在尝试找出将
WHERE
以及
添加到每个条件的逻辑,这取决于请求的条件数量

我的尝试:

($location == '' ? $location = '' : $location = '`location` = "'.$location.'"');
($expertise == '' ? $expertise = '' : $expertise = '`expertise` = "' . $expertise . '"');
($education == '' ? $education = '' : $education = '`education` = "' . $education . '"');
($location != '' && ($expertise != '' || $education != '') ? $and_1 = 'AND ' : $and_1 = '');
($location != '' && ($and_1 != '') ? $and_2 = 'AND ' : $and_2 = '');
($and_1 != '' || $and_2 != '' && $salary != 'no preference' ? $salary : '');
($and_1 == '' || $and_2 == '' && $salary != 'no preference' ? '' : $salary);
我想我只是迷失在我自己的心理逻辑中,因为我在编码,我甚至不理解我自己的代码逻辑lol;(

确切地说

$vars = array(
    ($location)?'`location` = "'.$location.'"'): null,
    /**
    ...
    ...
    **/
 );

function myfilterarray($var)
{
    return !empty($var)?$var: null;
}

$newvars = array_filter($vars, 'myfilterarray');

$where = join(" AND ", $newvars);
确切地说

$vars = array(
    ($location)?'`location` = "'.$location.'"'): null,
    /**
    ...
    ...
    **/
 );

function myfilterarray($var)
{
    return !empty($var)?$var: null;
}

$newvars = array_filter($vars, 'myfilterarray');

$where = join(" AND ", $newvars);

有HTML输入会有帮助,但这里有一个解决方案,它甚至不需要处理查询的某些部分

//do for each input
//or similar check to create empty string when there's not a valid input.
$input = is_empty($input) ? '' : $input;

对于一系列的薪水,你需要做一个检查,类似于你在问题中的想法

首先,将
$salary
设置为您需要的值,或者
$salary='no preference';
或者
$salary=array($num1,$num2);
。这应该在检查其他输入时完成

使用可以像
这样使用的东西构建基本查询:

$query = "SELECT *
          FROM staff
          WHERE location LIKE '%$location%'
          AND expertise LIKE '%$expertise%'
          AND education LIKE '%$education%'";
完成此操作后,基于$SALLAY的内容构建SALLAY子句:

if( is_array($salary) )
{
  $query .= " AND salary BETWEEN $salary[0] AND $salary[1]";
}
else
{
  $query .= " AND salary LIKE '%no preference%'";
  //or "AND salary = 'no preference'" if that field is exactly that
};

然后,您可以执行
$query
。注意,您需要对输入进行清理,以防用户试图在您的查询中进行恶意操作。

拥有HTML输入会有所帮助,但这里有一个解决方案,它甚至不需要将查询的某些部分删除或删除

//do for each input
//or similar check to create empty string when there's not a valid input.
$input = is_empty($input) ? '' : $input;

对于一系列的薪水,你需要做一个检查,类似于你在问题中的想法

首先,将
$salary
设置为您需要的值,或者
$salary='no preference';
或者
$salary=array($num1,$num2);
。这应该在检查其他输入时完成

使用可以像
这样使用的东西构建基本查询:

$query = "SELECT *
          FROM staff
          WHERE location LIKE '%$location%'
          AND expertise LIKE '%$expertise%'
          AND education LIKE '%$education%'";
完成此操作后,基于$SALLAY的内容构建SALLAY子句:

if( is_array($salary) )
{
  $query .= " AND salary BETWEEN $salary[0] AND $salary[1]";
}
else
{
  $query .= " AND salary LIKE '%no preference%'";
  //or "AND salary = 'no preference'" if that field is exactly that
};

然后你可以执行
$query
。注意,你需要清理你的输入,以防用户试图在你的查询中进行恶意操作。

ooh,duh!我甚至没有想过这样做,我被卡住了,并以编程方式构建查询。对于薪水,我有一个interval-caluse。我如何使用Like子句呢?$salary的结果为“无偏好”或20000-30000,这是使用$salary[0]和$salary[1]访问的结果爆炸后,我更新了我的答案。它不适合这里。另外,我建议你看看@experimentX给出的答案。我认为它最终会更灵活、更简洁。我的答案应该有效,可能更容易理解,但我认为他的答案更好、更有力。哦,duh!我甚至没有想到这样做的结果是,我被卡住了,并以编程的方式构建查询。对于salary,我有一个BETWEEN-caluse。我如何使用Like子句呢?对于$salary,结果要么是“无偏好”,要么是20000-30000,这是使用$salary[0]和$salary[1]访问的结果爆炸后,我更新了我的答案。它不适合这里。此外,我建议你研究一下@experimentX给出的答案。我认为它最终会更灵活、更简洁。我的答案应该有效,可能更容易理解,但我认为他的答案更好、更有力。