我有一个包含2个选择字段和1个输入的搜索表单,总共3个选项,因此我创建了一些if语句,取决于每个字段的设置,它具有自己的查询,但是它运行不正常,都是错误的,结果没有正确,它与语句查询混合在一起,这不是正确的。

这是我的搜索表单结果代码:

$keywords = $_GET["Keywords"];
             $location = $_GET['Location'];
             $jobtype = $_GET["Category"];


              if (isset($location) && empty($jobtype) && empty($keywords)){

              $sql_jobs = "SELECT * FROM jobs
                            WHERE
                            active = '1' AND
                            country = '$location'
                            ORDER BY id_job DESC";

                }elseif(isset($location) && isset($jobtype) && empty($keywords)){
                     $sql_jobs = "SELECT * FROM jobs
                            WHERE
                            active = '1' AND
                            country = '$location' AND
                            jobType_en = '$jobtype'
                            ORDER BY id_job DESC";

                }elseif(isset($location) && isset($jobtype) && isset($keywords)){
                     $sql_jobs = "SELECT * FROM jobs
                            WHERE
                            active = '1' AND
                            country = '$location' AND
                            jobType_en = '$jobtype' AND
                            title_en LIKE '%$keywords%'
                            ORDER BY id_job DESC";

                }elseif(empty($location) && isset($jobtype) && empty($keywords)){
                     $sql_jobs = "SELECT * FROM jobs
                            WHERE
                            active = '1' AND
                            jobType_en = '$jobtype'
                            ORDER BY id_job DESC";

                }elseif(empty($location) && isset($jobtype) && isset($keywords)){
                     $sql_jobs = "SELECT * FROM jobs
                            WHERE
                            active = '1' AND
                            jobType_en = '$jobtype' AND
                            title_en LIKE '%$keywords%'
                            ORDER BY id_job DESC";
                }elseif(empty($location) && isset($jobtype) && isset($keywords)){
                     $sql_jobs = "SELECT * FROM jobs
                            WHERE
                            active = '1' AND
                            jobType_en = '$jobtype' AND
                            title_en LIKE '%$keywords%'
                            ORDER BY id_job DESC";
                }
                else{
                     $sql_jobs = "SELECT * FROM jobs
                            WHERE
                            active = '1'
                            ORDER BY id_job DESC";
                }

                $consultaJob = mysql_query($sql_jobs);

最佳答案

您需要简化代码:

$sql = "SELECT * FROM jobs
                            WHERE
                            active = '1' AND
                            country = '$location' ";
$order = "ORDER BY id_job DESC";
$where = "";

if( isset( $jobtype ) { $where .= " AND jobType_en = '$jobtype'"; }
if( isset($keywords) { $where .= " AND title_en LIKE '%$keywords%'" };

$sqlx = $sql . $where . $order;


显然,您早些时候正在处理$ jobtype和$ keywords。在此处理中,您应该设置一些默认值,以使以后的处理更加容易。

$jobtitle = (isset( $_GET['jobtitle'] ) ) ? $_GET['jobtitle'] : "";
$keywords= (isset( $_GET['keywords'] ) ) ? $_GET['keywords'] : "";


这样您可以使用:

if( $jobtitle != '') { .... }

关于php - 搜索表单多输入,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29639476/

10-12 01:46