drruhc4944 2017-09-28 09:34
浏览 18
已采纳

当同一页面上发生某些错误时,下拉列表中的员工ID将消失

 public function getEmployeeIdForSalary(){
        if (!isset($_SESSION['ids'])){
            if (!isset($_SESSION['eids'])){
                $query = mysqli_query($this->connection, "SELECT EmployeeId from employees where EmployeeId NOT IN(Select EmployeeId from salary)ORDER BY EmployeeId ASC") or die("Query execution failed: " . mysqli_error());
                while ($row = $query->fetch_assoc()){
                    // Push the id to the array.
                    $_SESSION['ids'][] = $row["EmployeeId"];
                }
            }
        }
    }

and i am showing the above session value in separate file containing Html form and the code snippet is given below :

 <tr>
                <td>
                    Employee Code :
                </td>
                <td>
                   <select name="EmployeeId" id="EmployeeId" value='' class='form-control' required autofocus>
                         <?php
                        if (isset($_SESSION["ids"])) {         // For Insert
                           foreach ($_SESSION['ids'] as $e_id) {
                                echo "<option value='$e_id'>$e_id</option>";
                            }
                        }else {      // For Update
                            echo "<option value='$emp_id'>$emp_id</option>";
                        }

                        ?>
                    </select>
                </td>
            </tr>

For update operation when error occurs, Employee Id is retained in dropdown, but when i perform insert operation and then when some error occurs my 'Employee Id " gets disappear.

I want the Employee Id not to disappear when some error occurs while performing insert Operation.

Insert Code :

 if (!empty($_POST['done'])) { // To prevent data from Inserting in database on Page Refresh.
            if (!isset($_SESSION['salry'])) {
                $EmployeeId = mysqli_real_escape_string($this->connection, $_POST['EmployeeId']);
                $Salary = mysqli_real_escape_string($this->connection, $_POST['Salary']);
                $query = mysqli_query($this->connection, "SELECT EmployeeId FROM attendencestatus where EmployeeId='" . $EmployeeId . "'") or die("Query execution failed: " . mysqli_error());
                while ($rows = $query->fetch_array()){
                    $result = $rows["EmployeeId"];
                }
                $_SESSION["sal"] = $Salary;
                if ($EmployeeId === $result) {
                    if ((10000 <= $Salary) && ($Salary <= 80000)){  // Validation on Salary Text Field.
                        $_SESSION["idsss"] = $EmployeeId;
                        $sql = "Insert into salary(EmployeeId,Salary) VALUES ('$EmployeeId', '$Salary')";
                        $result_insert = mysqli_query($this->connection, $sql);
                        if (!$result_insert){
                            $_SESSION[error_salary] = array("Insertion Failed due to duplicate entry.");
                            header("Location:addSalary.php");
                        } else {
                            $_SESSION[insert_salary] = array("Congo, Salary of Employee is Successfully Added");
                            $_SESSION["sal"] = "";
                            header("location:showSalary.php");
                        }
                    } else {
                        $_SESSION['ers'] = array("Salary of Employee Must be between 10000 and 80000.");
                        header("Location:addSalary.php");
                    }
  • 写回答

1条回答 默认 最新

  • dongpu1331 2017-09-28 10:02
    关注
    <?php
    
       if (isset($_SESSION["ids"])) {         // For Insert
           foreach ($_SESSION['ids'] as $e_id) {
               $select = ($_SESSION['emp_id'] == $e_id ) ? 'selected' : ''; 
               /*check emp_id in session  to $e_id if equal then it is selected*/
               echo "<option value='$e_id'  $select  >$e_id</option>";
           }
       }else {      // For Update
    
           echo "<option value='$emp_id'>$emp_id</option>";
       }
     ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab