使用多个选择框过滤MySQL数据

时间:2014-10-19 20:58:51

标签: php mysql

我有一个PHP表格回显,显示我的所有数据。我试图根据下拉菜单和提交按钮来过滤数据。每个单独的下拉查询都可以100%运行...只需要自己没有elseif。也就是说,如果我逐个测试它们并注释掉其他过滤器的工作原理,那就不是当它们全部取消注释时。这是我的代码:

if ($_POST['raceName']!="Select a race") {
                $raceName = mysqli_real_escape_string($mysqli, $_POST["raceName"]);
                $filter = "SELECT a.MembershipID, a.FirstName, a.Surname, t.RaceID, t.Time, r.RaceID, r.RaceName, r.Distance, r.ClubYear
                            FROM Athlete AS a 
                            INNER JOIN Time AS t  
                                ON a.MembershipID=t.MembershipID
                            INNER JOIN Race AS r
                                ON t.RaceID=r.RaceID
                            WHERE r.RaceName= '$raceName'
                            ORDER BY a.Surname";
            } elseif ($_POST['athlete']!="Select an athlete") {
                $athlete = mysqli_real_escape_string($mysqli, $_POST["athlete"]);
                $firstWord = explode(' ',trim($athlete));
                $firstName = $firstWord[0];
                $lastWord = explode(' ', $athlete);
                $surname = array_pop($lastWord);
                $filter = "SELECT a.MembershipID, a.FirstName, a.Surname, t.RaceID, t.Time, r.RaceID, r.RaceName, r.Distance, r.ClubYear  
                            FROM Athlete AS a 
                            INNER JOIN Time AS t  
                                ON a.MembershipID=t.MembershipID
                            INNER JOIN Race AS r
                                ON t.RaceID=r.RaceID
                            WHERE a.FirstName= '$firstName' AND a.Surname = '$surname'
                            ORDER BY a.Surname";
            } elseif ($_POST['distance']!="Select a distance") {
                $distance = mysqli_real_escape_string($mysqli, $_POST["distance"]);
                $filter = "SELECT a.MembershipID, a.FirstName, a.Surname, t.RaceID, t.Time, r.RaceID, r.RaceName, r.Distance, r.ClubYear 
                            FROM Athlete AS a 
                            INNER JOIN Time AS t  
                                ON a.MembershipID=t.MembershipID
                            INNER JOIN Race AS r
                                ON t.RaceID=r.RaceID
                            WHERE r.Distance= '$distance'
                            ORDER BY a.Surname";
            } elseif ($_POST['clubYear']!="Select a club year") {
                $clubYear = mysqli_real_escape_string($mysqli, $_POST["clubYear"]);
                $filter = "SELECT a.MembershipID, a.FirstName, a.Surname, t.RaceID, t.Time, r.RaceID, r.RaceName, r.Distance, r.ClubYear
                            FROM Athlete AS a 
                            INNER JOIN Time AS t  
                                ON a.MembershipID=t.MembershipID
                            INNER JOIN Race AS r
                                ON t.RaceID=r.RaceID
                            WHERE r.ClubYear= '$clubYear'
                            ORDER BY a.Surname";
            } else {
                $filter = "SELECT a.MembershipID, a.FirstName, a.Surname, t.RaceID, t.Time, r.RaceID, r.RaceName, r.Distance, r.ClubYear 
                                FROM Athlete AS a 
                                INNER JOIN Time AS t  
                                ON a.MembershipID=t.MembershipID
                                INNER JOIN Race AS r
                                ON t.RaceID=r.RaceID
                                ORDER BY a.Surname";
            }

            $result = $mysqli->query($filter);

正如您所看到的,我将它们与elseif相结合,因为我打算将查询组合在一起,以便用户可以使用任意数量的下拉菜单进行过滤。现在我只是喜欢每个人自己工作,但使用这个elseif方法。目前,此代码适用于raceName,但没有别的。它只返回一张空白表。有什么想法吗?

2 个答案:

答案 0 :(得分:0)

因为第一段代码运行它并没有到达elseif。请改为使用switch语句,或者只是取出其他内容

答案 1 :(得分:0)

我设法解决这个问题,而不是检查下拉列表是否等于下拉列表所说的内容,检查下拉列表是否为空。然后我删除了else语句并组合了查询。完整代码如下:

$query = "SELECT a.MembershipID, a.FirstName, a.Surname, t.RaceID, t.Time, r.RaceID, r.RaceName, r.Distance, r.ClubYear
                            FROM Athlete AS a 
                            INNER JOIN Time AS t  
                                ON a.MembershipID=t.MembershipID
                            INNER JOIN Race AS r
                                ON t.RaceID=r.RaceID";
            if ($_POST['raceName']!= null) {
                $raceName = mysqli_real_escape_string($mysqli, $_POST["raceName"]);
                $filter = " WHERE r.RaceName= '$raceName'";
                $query .= $filter;
            } 
            if ($_POST['athlete']!= null) {
                $athlete = mysqli_real_escape_string($mysqli, $_POST["athlete"]);
                $firstWord = explode(' ',trim($athlete));
                $firstName = $firstWord[0];
                $lastWord = explode(' ', $athlete);
                $surname = array_pop($lastWord);
                $filter = " WHERE a.FirstName= '$firstName' AND a.Surname = '$surname'";
                $query .= $filter;
            } 
            if ($_POST['distance']!= null) {
                $distance = mysqli_real_escape_string($mysqli, $_POST["distance"]);
                $filter = " WHERE r.Distance= '$distance'";
                $query .= $filter;
            } 
            if ($_POST['clubYear']!= null) {
                $clubYear = mysqli_real_escape_string($mysqli, $_POST["clubYear"]);
                $filter = " WHERE r.ClubYear= '$clubYear'";
                $query .= $filter;
            }

            $fullquery = $query . " ORDER BY a.Surname";

            $result = $mysqli->query($fullquery);

现在唯一可以理解的是,如何插入" AND"在WHERE查询之间是否有多个。