搜索查询未返回所需结果

时间:2016-10-08 09:36:07

标签: php mysql

我有一些高级搜索功能。除了AND LIKE ...之外,一切正常。 有两个选项AA, *AB。我希望当我选择A只返回数据库中A的结果时,以及当我选择A, *AB返回仅A, *AB的结果时,我想要。

现在发生的事情是,当我选择B, *AB时,它会返回两者的结果

A
A, *AB

我的表单是行动get。这是我的查询

    $couGe=($ge == "None") ? "" : "AND c.ge LIKE '".substr($ge, 0, 2)."%'";
    $active = ($this->showCouCancelled) ? "AND (c.active=1 OR c.active=7)" : "";
    $active = ($this->hideCouActive) ? "AND (c.active=1 OR c.active=3 OR c.active=4 OR c.active=7)" : "";
    $couName = ($cName) ? "AND course_title LIKE '%$cName%'" : "";
    $couDept = ($cDept) ? "AND s.dept='$cDeptArray[0]' AND s.dept_code='$cDeptArray[1]'" : "";

    $iRes = _SQLQuery("
                    SELECT DISTINCT c.id,s.dept,s.dept_code,c.course_title,c.active,s.year,s.sem,c.ge,c.course_type
                    FROM courses AS c, sections AS s, teachers as t, tea_sec_rel as tsr
                    WHERE s.course_id_rel=c.id AND $curYr $curSem $active $couName $couDept $psSearch $couGe
                    ORDER BY s.dept,s.dept_code");

html选择选项菜单有

<select name="ge" id="ge">
   <option value="None">All</option>
   <option value="A">A</option>
   <option value="A, *AB">A, *AB</option>
</select>

在查询中,这是采用ge

的行
$couGe=($ge == "None") ? "" : "AND c.ge LIKE '".substr($ge, 0, 2)."%'";

当我var_dump($_GET['ge']时,我发现我得到了我所选择的。我非常困惑,感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

您需要更改一下查询以及分配$couGe所选ge的位置。请尝试这种方式

$couGe=($ge == "None") ? "" : "'".substr($ge, 0, 2)."'";
$active = ($this->showCouCancelled) ? "AND (c.active=1 OR c.active=7)" : "";
$active = ($this->hideCouActive) ? "AND (c.active=1 OR c.active=3 OR c.active=4 OR c.active=7)" : "";
$couName = ($cName) ? "AND course_title LIKE '%$cName%'" : "";
$couDept = ($cDept) ? "AND s.dept='$cDeptArray[0]' AND s.dept_code='$cDeptArray[1]'" : "";

$iRes = _SQLQuery("
                SELECT DISTINCT c.id,s.dept,s.dept_code,c.course_title,c.active,s.year,s.sem,c.ge,c.course_type
                FROM courses AS c, sections AS s, teachers as t, tea_sec_rel as tsr
                WHERE s.course_id_rel=c.id AND $curYr $curSem $active $couName $couDept $psSearch $couTea AND c.ge LIKE $couGe
                ORDER BY s.dept,s.dept_code");