在刷新时,值会继续添加下拉列表

时间:2017-09-17 09:22:49

标签: php

代码:

public function getEmployeeId() {
        if (!isset($_SESSION["email"]) || !isset($_SESSION["passwrd"])) {
            header("Location:index.php");
            // Cannot Access this page without Login.
        }


            $_SESSION['ids'][] = "";
            $query = mysqli_query($this->connection, "SELECT EmployeeId from employees where EmployeeId NOT IN(Select EmployeeId from employeeprofile)") or die("Query execution failed: " . mysqli_error());
            while ($row = $query->fetch_assoc()) {
                // Push the id to the array.
                $_SESSION['ids'][] = $row["EmployeeId"];
            }


    }

值来自数据库,每当我刷新重复值时都会添加。我不知道该怎么做。

1 个答案:

答案 0 :(得分:1)

那是因为每次刷新页面时(可能每次getEmployeeId()方法调用)都会执行SQL查询,并将员工ID附加到$_SESSION['ids']数组。您需要在<{1}}块中封装 代码块。

if

旁注:您需要在public function getEmployeeId() { if (!isset($_SESSION["email"]) || !isset($_SESSION["passwrd"])) { header("Location:index.php"); exit(); } if(!isset($_SESSION['ids']) && count($_SESSION['ids']) == 0){ $_SESSION['ids'][] = ""; $query = mysqli_query($this->connection, "SELECT EmployeeId from employees where EmployeeId NOT IN(Select EmployeeId from employeeprofile)") or die("Query execution failed: " . mysqli_error()); while ($row = $query->fetch_assoc()) { $_SESSION['ids'][] = $row["EmployeeId"]; } } } 语句后添加exit();,因为单独header(...);本身不足以将用户重定向到其他页面。< / p>