Leave Entry Form I am not able to find a way to compare the selected leave type $typ === 2 with the Sick leave id.
The $typ = $_POST[‘typ’] store the leave type obtained from a database such as Annual leave and Sick Leave. If I select the leave type from a form and it is a sick leave then I would just need to insert leave_date and resume_date in leave table otherwise if the selected leave type is annual then I need to insert the leave_date, resume_date in the leave table as well to update the leave_entitlement table just to reduce leave_entitlement balance
This is a excerpt from my form
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" >
<div class="row form-group">
<div class="col-lg-12">
<label>Leave Type</label>
<select size="1" name="typ">
<?php
$typs = mysqli_query($dbconn,"select * from type") or die('Query
failed: ' . mysqli_error());
while ($line = mysqli_fetch_array($typs))
{
echo "<option value='" . $line['typ_id'] . "'>";
echo $line['typ_name'] . "</option>";
}
?>
</select>
</div>
</div>
</form>
This my submit
if(isset($_POST['submit']))
{
$empnum = $_POST['empnum'];
// echo $empnum;
$leave_date = $_POST['leave_date'];
$resume_date = $_POST['resume_date'];
$typ = $_POST['typ'];//leave type e.g Annual Leave, Sick Leave
// this is a function to get the number of leave days from a form
Function GetNoOfDays($leave_date,$resume_date){
$leave_dater = $leave_date;
$resume_dater = $resume_date;
$datediff = $resume_date - $leave_date;
return round($datediff / (60 * 60 * 24));
}
echo $leavetaken = GetNoOfDays(strtotime($leave_date),strtotime($resume_date));
//if a leave type is annual leave, $qry1 is excuted in the if body otherwise
//if a leave type is sick leave $qry1 and qry2 in the else are excuted
if($typ === 2) // 2 is a sick leave id if leave type is equal to 2 (sick leave)
{
$qry1 = "INSERT INTO eleave VALUES(''," .
"'$empnum','$leave_date','$resume_date'," .
"'$typ')";
}else{
$qry1 = "INSERT INTO eleave VALUES(''," .
"'$empnum','$leave_date','$resume_date'," .
"'$typ')";
$qry2 = "UPDATE eleave_entitlement SET annual_entitlement = annual_entitlement-$leavetaken WHERE emp_num = $empnum";
}
// execute query
$added = mysqli_query($dbconn,$qry1).mysqli_query($dbconn,$qry2);
// report results
if(($added) != "")
echo "Record added successfully." . "<br>";
else
{
echo "ERROR: Record could not be added<br>" .
mysqli_error($dbconn);
}
// close connection
mysqli_close($dbconn);
}
?>
The problem is when I select Annual Leave or Sick Leave from the form my else part of my if statement always executed. Meaning that the leave_entitlement balance is always reduced even when I select Sick Leave
Advertisement
Answer
You use comparator ===
which will compare both value and data type. The data type of $typ is String so "2" === 2
will return false. If you want to use ===
you can cast $typ to integer by using (int) $typ
or just change ===
to ==
.