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 用verilog实现tanh函数和softplus函数
  • ¥15 求京东批量付款能替代天诚
  • ¥15 slaris 系统断电后,重新开机后一直自动重启
  • ¥15 51寻迹小车定点寻迹
  • ¥15 谁能帮我看看这拒稿理由啥意思啊阿啊
  • ¥15 关于vue2中methods使用call修改this指向的问题
  • ¥15 idea自动补全键位冲突
  • ¥15 请教一下写代码,代码好难
  • ¥15 iis10中如何阻止别人网站重定向到我的网站
  • ¥15 滑块验证码移动速度不一致问题