Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/254.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何为搜索筛选添加动态WHERE子句?_Php_Sql Server - Fatal编程技术网

Php 如何为搜索筛选添加动态WHERE子句?

Php 如何为搜索筛选添加动态WHERE子句?,php,sql-server,Php,Sql Server,我有7个搜索参数,尽管下面的代码只显示了两个,标题和类型 我们想让我们的用户能够通过7个参数中的任何一个进行搜索 还应赋予它们通过多个参数进行搜索的能力 我如何调整下面的代码以使用动态$where子句 例如,用户可以选择type='some value'的位置 用户还应该能够选择type='some value'和title='some value'的位置 先谢谢你 function ms_escape_string($data) { if ( !isset($data) or

我有7个搜索参数,尽管下面的代码只显示了两个,标题和类型

我们想让我们的用户能够通过7个参数中的任何一个进行搜索

还应赋予它们通过多个参数进行搜索的能力

我如何调整下面的代码以使用动态$where子句

例如,用户可以选择type='some value'的位置

用户还应该能够选择type='some value'和title='some value'的位置

先谢谢你

function ms_escape_string($data) {
        if ( !isset($data) or empty($data) ) return '';
        if ( is_numeric($data) ) return $data;

        $non_displayables = array(
            '/%0[0-8bcef]/',            // url encoded 00-08, 11, 12, 14, 15
            '/%1[0-9a-f]/',             // url encoded 16-31
            '/[\x00-\x08]/',            // 00-08
            '/\x0b/',                   // 11
            '/\x0c/',                   // 12
            '/[\x0e-\x1f]/'             // 14-31
        );
        foreach ( $non_displayables as $regex )
            $data = preg_replace( $regex, '', $data );
        $data = str_replace("'", "''", $data );
        return $data;
    }

    $strprojectTitle = null;
    $strbidType = null;
    if(isset($_POST["projectTitle"]))
    {
        $strprojectTitle = $_POST["projectTitle"];
    }
    if(isset($_POST["BidType"]))
    {
        $strbidType = $_POST["BidType"];
    }

?>

<?php

   $sql = "Select b.ID,convert(char(10),b.BidDate,101) BidDate,convert(char(10),
          b.DueDate,101)DueDate,b.BidTitle,b.DueTime,b.BidID,BidIDFile,
          d.Department,b.BidType,CASE WHEN b.AwardDate ='01/01/1900' Then NULL ELSe convert(char(10),b.AwardDate,101)END AS AwardDate,
          convert(char(10),b.LastUpdate,101) LastUpdate,s.Status
          FROM bids b inner join dept d on b.Department=d.DeptID inner join Status s on b.BidStatus=s.StatusId WHERE b.BidTitle = ' . ms_escape_string($strprojectTitle) . ' OR b.BidType = ' . ms_escape_string($strbidType) . ' ";

在代码中删除此部分:

$strprojectTitle = null;
$strbidType = null;
if(isset($_POST["projectTitle"]))
{
    $strprojectTitle = $_POST["projectTitle"];
}
if(isset($_POST["BidType"]))
{
    $strbidType = $_POST["BidType"];
}
并将其替换为下面的一个,它可以动态地构建WHERE条件:

## easily add here the fields you have
$fields = array(
    'projectTitle' => 'b.BidTitle',
    'BidType' => 'b.BidType'
);

$where = array();
foreach($fields as $fieldPost => $fieldDb) {
    if(isset($_POST[$fieldPost]) && strlen($_POST[$fieldPost]) > 0) {
        $where[] = "`$fieldDb` = '" . ms_escape_string($_POST[$fieldPost]) . "'";
    }
}

## Use the $where array in your final SQL query
## important to test if count($where) > 0 in case no search has been made
$sql = "SELECT ..... " . ( count($where) > 0 ? " WHERE " . implode(' AND ', $where) : "" );
一个更高级的示例,通过支持多种搜索类型来构建WHERE字符串(例如搜索比较器:如%..%和=)


查询生成器或if语句:)您可以有一个带有可选参数的存储过程看看这个答案我可能会在imploding@engine9pw哇,难以置信的回答。只是一个问题,我喜欢更高级的选项,但这是否意味着
'BidType'=>数组('field'=>'b.BidType','searchType'=>'equal')
BidType是表单字段,而不是db。db字段名为b.BidType。请容忍新观点。非常感谢大家。嗨,不客气。我很高兴你喜欢这个解决方案。我刚从原始查询中获取了db字段名:或b.BidType=。。。。。。。你们可以使用你们喜欢的任何东西。@engine9pw,嗨,请看我在页面顶部的最新帖子。我遇到了一个错误,上面写着:
警告:sqlsrv\u num\u rows()希望参数1是资源,布尔值在…
中给出。现在,我已经删除了ms\u escapte\u String()函数。这给我带来了麻烦。我将恢复它一旦我得到的代码工作。Giorgos,非常感谢你的努力。我想要一些类似engin9pw给我的东西。它仅在调用时使用和。否则,请使用用户想要的过滤器。请大家耐心点。你们可以试着调试你们的查询。仅显示查询字符串(echo$sql),查看是否可以发现任何错误,甚至可以在单独的sql客户机中运行它。
## easily add here the fields you have
$fields = array(
    'projectTitle' => 'b.BidTitle',
    'BidType' => 'b.BidType'
);

$where = array();
foreach($fields as $fieldPost => $fieldDb) {
    if(isset($_POST[$fieldPost]) && strlen($_POST[$fieldPost]) > 0) {
        $where[] = "`$fieldDb` = '" . ms_escape_string($_POST[$fieldPost]) . "'";
    }
}

## Use the $where array in your final SQL query
## important to test if count($where) > 0 in case no search has been made
$sql = "SELECT ..... " . ( count($where) > 0 ? " WHERE " . implode(' AND ', $where) : "" );
## support multiple search comparators
$fields = array(
    'projectTitle' => array('field' => 'b.BidTitle', 'searchType' => 'like'),
    'BidType' => array('field' => 'b.BidType', 'searchType' => 'equal')
);

$where = array();
foreach($fields as $fieldPost => $field) {
    if(isset($_POST[$fieldPost]) && strlen($_POST[$fieldPost]) > 0) {
        if($field['searchType'] == 'like') {
            $where[] = "`".$field['field']."` LIKE '%" . ms_escape_string($_POST[$fieldPost]) . "%'";
        } else {
            $where[] = "`".$field['field']."` = '" . ms_escape_string($_POST[$fieldPost]) . "'";
        }
    }
}