动态PHP查询-过滤器选择

动态PHP查询-过滤器选择,php,mysql,dynamic,filter,Php,Mysql,Dynamic,Filter,好的,伙计们,我还需要你们的帮助,如果可以的话,请再说一遍 我有一个下拉选项列表作为一组筛选条件 选中每个后,它会将其添加到查询字符串中,并在选择中附加一个等于(=)的值,因此如果选择“大”,则查询将位于size=Large的位置 我有一个选择称为impacted,但当用户选择impacted时,我不希望查询附加到append=to,我希望它成为受影响的位置,如“单击”,这样它将搜索数据库列中受影响的任何位置 如果保持为=至,则只会将仅针对课程存储了一个受影响系统的选择返回到该选择 SELEC

好的,伙计们,我还需要你们的帮助,如果可以的话,请再说一遍

我有一个下拉选项列表作为一组筛选条件

选中每个后,它会将其添加到查询字符串中,并在选择中附加一个等于(=)的值,因此如果选择“大”,则查询将位于size=Large的位置

我有一个选择称为impacted,但当用户选择impacted时,我不希望查询附加到append=to,我希望它成为受影响的位置,如“单击”,这样它将搜索数据库列中受影响的任何位置

如果保持为=至,则只会将仅针对课程存储了一个受影响系统的选择返回到该选择

 SELECT p.project_id 
      , p.project_name
      , p.department
      , p.size
      , p.programme
      , l.captured_by
      , l.stage
      , l.type
      , l.impacted
      , l.depts_inv
      , l.lesson_title
      , l.lesson_learned
      , l.lesson_added 
   FROM ll_project p
   JOIN ll_lessons l
     ON l.project_id = p.project_id 
    AND l.impacted = 'Click';
这就是它当前将代码拉入的方式,但是如果有人针对受影响的筛选器进行了选择,我希望查询不要在其前面添加=而是“包含”或“类似”,如果您知道我的意思的话

这是完整的代码

<?php 
//Set all Variables pulled from POST of previous page
$pid = $_POST['project_id'] ;
$psize = $_POST['projectSize'] ;
$pdepts = $_POST['depts'] ;
$lstage = $_POST['stage'] ;
$ltype = $_POST['type'] ;
$impacted = $_POST['impacted'] ;
//Create Column Variable to hold Values in an array linked to the database columns
$columns = array('project_id'=>'ll_project.project_id','projectSize'=>'size','depts'=>'department','stage'=>'ll_lessons.stage','type'=>'ll_lessons.type','impacted'=>'ll_lessons.impacted');

$sqlString = null;
//Check all POSTED data is pulling through - Should be 6
//echo "Total Number Of Captured Post Variables is:";
//echo count($_POST);

$number = 0;
$queryStr = ""; 
$preStr = array(); 
//For Every POSTED Value Set the Name as Key and the Value against Each
foreach ($_POST as $key => $val ) {

if (!empty($_POST[$key])){
       if(!is_array($_POST[$key]))
       //Escape the VALUE by surrounding it with Quotes as it is a string the value would not be picked up on the query.
           $currentStr = $columns[$key]." = '".mysql_real_escape_string($val)."'"; 
       else
       $currentStr = $columns[$key]." IN (".implode(',',$_POST[$key]).")"; 
       $preStr[] = $currentStr; 
   }
 }

//set the Query String that i want to return from the Database and set the join on the BSKYB NUMBERS on both Tables
$queryStr = "SELECT ll_project.project_id, ll_project.project_name, ll_project.department, ll_project.size, ll_project.programme, ll_lessons.captured_by, ll_lessons.stage, ll_lessons.type, ll_lessons.impacted, ll_lessons.depts_inv, ll_lessons.lesson_title, ll_lessons.lesson_learned, ll_lessons.lesson_added FROM ll_project INNER JOIN ll_lessons ON ll_project.project_id = ll_lessons.project_id  WHERE ".implode(' AND ',$preStr);
答案是这样的

if ($key == 'impacted') {
       $currentStr = $columns[$key] . " LIKE '%" . mysql_real_escape_string($val) . "%'";
     } else {
       $currentStr = $columns[$key] . " = '" . mysql_real_escape_string($val) . "'"; 
     }
       else
       $currentStr = $columns[$key]." IN (".implode(',',$_POST[$key]).")"; 
       $preStr[] = $currentStr; 
   }
}

谢谢你查看草莓:)

我真的不明白你在找什么。为什么不提供一个sqlfiddle呢?另外,虽然转义数据很好,但请注意,PHP的mysql方法现在已被弃用,取而代之的是PDO和mysqli。代码的第一部分是由下面的代码根据过滤器选择选项动态生成的。如果用户选择了受影响的过滤器,我希望查询像“the_selection”一样写入受影响的过滤器,而不是像它当前的工作方式一样写入受影响的过滤器=“the_selection”肯定与currentStr=$columns[$key]。“=”。mysql_real_escape_字符串($val)。“;因此,必须检查是否选择了impacted,然后附加一个LIKE操作符而不是一个=这难道不像将“=”替换为“LIKE”并将$val包装在其中两个操作符中那样简单吗?“%”?