在连接查询中使用多个and条件

btqmn9zl  于 2021-06-20  发布在  Mysql
关注(0)|答案(5)|浏览(377)

如果post中的值不等于blank,并且当post中的值和post中的值都为空时,我希望连接查询。问题是当值只在一个post中时 AND 在任何其他第二个值之前。希望得到解决。我使用的是核心php

if(!empty($_POST['agent_wise'])){       
    $agent_wise = $_POST['agent_wise'];
    $queryCondition .= "allusers.sno ='$agent_wise'";
} 

if(!empty($_POST['status_wise'])){      
    $status_wise = $_POST['status_wise'];
    $queryCondition .= " AND st_application.admin_status_crs ='$status_wise'";
}   
if(!empty($_POST['course_wise'])){      
    $course_wise = $_POST['course_wise'];
    $queryCondition .= " AND st_application.prg_name1 ='$course_wise'";
}

$result2 = "SELECT * FROM st_application INNER JOIN allusers on st_application.user_id = allusers.sno where " . $queryCondition . " ";
kupeojn6

kupeojn61#

懒惰的黑客包括 AND 在第一种情况下,也会发生变化 WHEREWHERE 1 但我怀疑,有更聪明、更容易搜索的方法来解决这个问题。哦,还要看看sql注入和准备语句的重要性

8e2ybdfx

8e2ybdfx2#

这是解决办法。。。

$flag = 0;
if(!empty($_POST['agent_wise'])){ 
        $flag = 1;    
        $agent_wise = $_POST['agent_wise'];
        $queryCondition .= "allusers.sno ='$agent_wise'";
     } 

if(!empty($_POST['status_wise'])){      
        $status_wise = $_POST['status_wise'];
        if($flag == 1){
        $queryCondition .= " AND st_application.admin_status_crs ='$status_wise'";
        }
      else {
        $queryCondition .= " st_application.admin_status_crs ='$status_wise'";
         $flag = 1;
  }
    }   
    if(!empty($_POST['course_wise'])){      
        $course_wise = $_POST['course_wise'];
       if ($flag == 1){
        $queryCondition .= " AND st_application.prg_name1 ='$course_wise'";
      }
       else {
           $queryCondition .= " st_application.prg_name1 ='$course_wise'";
    }
    }

$result2 = "SELECT * FROM st_application INNER JOIN allusers on st_application.user_id = allusers.sno where " . $queryCondition . " ";
qlfbtfca

qlfbtfca3#

试试这个代码。

if(!empty($_POST['agent_wise'])){      
    $agent_wise = $_POST['agent_wise'];
    $queryCondition .= "allusers.sno ='$agent_wise'";
 } 

if(!empty($_POST['status_wise'])){      
    $status_wise = $_POST['status_wise'];
    $queryCondition .= " AND st_application.admin_status_crs ='$status_wise'";
}   

if(!empty($_POST['course_wise'])){      
    $course_wise = $_POST['course_wise'];
    $queryCondition .= " AND st_application.prg_name1 ='$course_wise'";
}

$queryCondition = trim($queryCondition, 'AND'); 

$result2 = "SELECT * FROM st_application INNER JOIN allusers on st_application.user_id = allusers.sno where " . $queryCondition . " ";

希望对你有帮助

wlzqhblo

wlzqhblo4#

在concating“and”之前检查空字符串。

$queryCondition .=  (empty($queryCondition) ? "" : " AND ") . 
    "st_application.admin_status_crs ='$status_wise'"
rjjhvcjd

rjjhvcjd5#

你的问题有两种解决办法。一个sql解决方案和一个php解决方案:
第一个解决方案

if(!empty($_POST['agent_wise'])){       
    $agent_wise = $_POST['agent_wise'];
    $queryCondition .= " AND allusers.sno ='$agent_wise'";
} 

if(!empty($_POST['status_wise'])){      
    $status_wise = $_POST['status_wise'];
    $queryCondition .= " AND st_application.admin_status_crs ='$status_wise'";
}   
if(!empty($_POST['course_wise'])){      
    $course_wise = $_POST['course_wise'];
    $queryCondition .= " AND st_application.prg_name1 ='$course_wise'";
}

$result2 = "SELECT * FROM st_application INNER JOIN allusers on st_application.user_id = allusers.sno where TRUE" . $queryCondition . " ";

这是因为在“真”和“其他”的情况下,真条件是多余的。唯一的缺点是,如果所有的帖子都不存在,那么您将获得所有的记录,因为where true总是返回记录。但你可以很容易地做一个检查,看看所有的职位都存在,如果没有:

if (empty($_POST['agent_wise']) && empty($_POST['status_wise']) && empty($_POST['course_wise']) ) exit;

第二种解决方案

$queryConditionsArray = [];

if(!empty($_POST['agent_wise'])){       
    $agent_wise = $_POST['agent_wise'];
    $queryConditionsArray[] = "allusers.sno ='$agent_wise'";
} 

if(!empty($_POST['status_wise'])){      
    $status_wise = $_POST['status_wise'];
    $queryConditionsArray[] = "st_application.admin_status_crs ='$status_wise'";
}   
if(!empty($_POST['course_wise'])){      
    $course_wise = $_POST['course_wise'];
    $queryConditionsArray[] = "st_application.prg_name1 ='$course_wise'";
}

$queryCondition = implode(' AND ', $queryConditionsArray);

$result2 = "SELECT * FROM st_application INNER JOIN allusers on st_application.user_id = allusers.sno where " . $queryCondition . " ";

这是因为内爆只会在条件之间插入单词“and”,而不是在每个条件的开头。同样,您必须检查是否没有post,否则您的sql将失败。
就我个人而言,我更喜欢第二种方式,但这要视情况而定。

相关问题