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");
}