Hi,
I'm hoping someone can help me with my PHP script for entering long term leave into a database.
I have a script where a user first selects a name from a drop down menu and then selects the type of leave in another drop down menu (e.g. Maternity, Sick, unpaid leave etc)
The final part before submitting to the database is to select a start date (DD-MM-YYYY) and an end date (DD-MM-YYYY). This is where I have a problem :queasy:
I do not know how to submit a load of dates into a database from a selected date range.
Here's all my PHP script at present:-
<?php
// File : enter_long_term_leave.php
// Title : Enter Long Term Leave
// Desc : Used to enter long term leave using a date range.
// Date : 09-09-09
include('staffdb_fns1.php');
require_once('classes/tc_calendar.php');
session_start();
security_check ($_SESSION[$NAME.' - level'],3, NULL);
//Check for default staff and/or section
$default_section = $_SESSION['default_section'];
if (isset($_POST['submit']))
{
// Get submitted form data
$staff_id = $_POST['staff_id'];
$type_of_leave = $_POST['type_of_leave'];
$leave_day = $_POST['leave_day'];
$leave_month = $_POST['leave_month'];
$leave_year = $_POST['leave_year'];
$enter_date = new_build_date ($leave_day, $leave_month, $leave_year);
$enter_date = addslashes ($enter_date);
$last_leave_day = $_POST['last_leave_day'];
$last_leave_month = $_POST['last_leave_month'];
$last_leave_year = $_POST['last_leave_year'];
$enter_last_date = new_build_date ($last_leave_day, $last_leave_month, $last_leave_year);
$enter_last_date = addslashes ($enter_last_date);
// All data entered
if (!$staff_id || !$type_of_leave || !$enter_date || !$enter_last_date )
{
echo "<script>";
echo "alert ('Not all information entered!')";
echo "</script>";
print "<META HTTP-EQUIV=\"Refresh\" CONTENT=\"0;URL=.enter_long_term_leave.php\"> ";
exit;
} //End of if ($staff_id || $type_of_leave)
// Check date is not in past
$today = date('Y-m-d');
if (($_SESSION['privilege_level'] > 2) && ($enter_date < $today))
{
echo "<script>";
echo "alert ('Cannot enter leave retrospectively!')";
echo "</script>";
print "<META HTTP-EQUIV=\"Refresh\" CONTENT=\"0;URL=.enter_long_term_leave.php\"> ";
exit;
} //End of if (($_SESSION['privilege_level']>2) && ($today>$tempTs))
//check if staff already taken this type of leave on that date
if (get_leave_type($staff_id,$enter_date,$type_of_leave,$db))
{
echo "<script>";
echo "alert ('Staff member has already taken leave for a day within that period. Try again!')";
echo "</script>";
print "<META HTTP-EQUIV=\"Refresh\" CONTENT=\"0;URL=.enter_long_term_leave.php\"> ";
exit;
}
// Enter Leave into DB
// Connect to DB
if (!$db)
{
echo "<script>";
echo "alert ('Could not connect to database. See system administrator!')";
echo "</script>";
print "<META HTTP-EQUIV=\"Refresh\" CONTENT=\"0;URL=.enter_long_term_leave.php\"> ";
exit;
} //End of (!$db)
mysqli_select_db($db, $NAME);
// Choose which table
switch ($type_of_leave)
{
case AL:
//Insert data into annual leave
$query = "INSERT INTO $NAME.annual_leave_taken SET " .
"staff_id =$staff_id, " .
"leave_hrs ='7.5', " .
"leave_date ='$enter_date'" ;
break; //End of case AL
case IL:
//Insert data into inlue leave taken
$query = "INSERT INTO $NAME.inlieu_leave_taken SET " .
"staff_id =$staff_id, " .
"leave_hrs ='7.5', " .
"leave_date ='$enter_date'" ;
break; //End of case IL
default:
//Insert data into other leave taken
$query = "INSERT INTO $NAME.other_leave_taken SET " .
"staff_id =$staff_id, " .
"leave_hrs ='7.5', " .
"leave_date ='$enter_date', " .
"leave_code ='$type_of_leave'";
} // End of switch ($type_of_leave)
// ?AL -> do they have enough left
if ($type_of_leave == 'AL') {
$al_allow = get_al_allowance($staff_id, $enter_date, $db);
$al_taken = get_al_taken($staff_id, $enter_date, $db);
if (($al_allow - $taken_total - $no_of_hours) < 0.1) {
echo "<script>";
echo "alert ('Not enough A/L remaining! This staff member has ".($al_allow - $taken_total)." hours of AL! Check AL entitlement.')";
echo "</script>";
print "<META HTTP-EQUIV=\"Refresh\" CONTENT=\"0;URL=./enter_long_term_leave.php\"> ";
exit;
}
}
// Perform SQL
$result = mysqli_query($db, $query);
if (!$result)
{
echo "<script>";
echo "alert ('There was a problem inserting data into database. See system administrator!')";
echo "</script>";
print "<META HTTP-EQUIV=\"Refresh\" CONTENT=\"0;URL=./enter_long_term_leave.php\"> ";
} //End of if (!$result)
else
{
echo "<script>";
echo "alert ('Record successfully inserted')";
echo "</script>";
print "<META HTTP-EQUIV=\"Refresh\" CONTENT=\"0;URL=./enter_long_term_leave.php\"> ";
} //End of else if (!$result)
$_SESSION['staff_id'] = FALSE;
} //End of if (isset($_POST['submit']))
?>
<html>
<head>
<title>Enter Long Term Leave</title>
<style type="text/css">
<?php
//get date information
$currentDate = mktime();
$date = getdate($currentDate);
$day = $date['mday'];
$month = $date['mon'];
$year = $date['year'];
//instantiate class and set properties
$myCalendar = new tc_calendar("leave", true);
$myCalendar->setIcon("images/iconCalendar.gif");
$myCalendar->setDate($day, $month, $year);
$myCalendar->setYearInterval(($year - 1), ($year + 1));
?>
</STYLE>
<script language="javascript" src="calendar.js"></script>
</head>
<body>
<h1>Enter Long Term Leave Form</h1>
<table cellspacing="0" cellpadding="10" border="1" bgcolor="lightblue">
<tr>
<td><a href='./index.php'>Homepage</a></td>
<td><a href='../menu.php'>Return to Portal</a></td>
</tr>
</table>
<form action = "<?php echo $PHP_SELF ?>" method = "post">
<table border = "0">
<tr>
<td>Staff Name:</td>
<td>
<select name = "staff_id" id = "staff_id" />
<?php
$options = user_staff_name_box_employed($default_section, $db);
echo $options;
?>
</select>
</td>
</tr>
<tr>
<td>Type Of Leave:</td>
<td>
<select name = "type_of_leave" id = "type_of_leave" />
<?php
$options = leave_type_box($db);
echo $options;
?>
</select>
</td>
</tr>
<?php
echo '<tr>';
echo '<td> Start Date:</td>';
echo '<td>';
//output the calendar
$myCalendar->writeScript();
echo '</td>';
echo '</tr>';
?>
<?php
echo '<tr>';
echo '<td> End Date:</td>';
echo '<td>';
//output the calendar
$myCalendar->writeScript();
echo '</td>';
echo '</tr>';
?>
<td colspan = "2" align = "center"><br />
<input type = "submit" name = "submit" value = "Save" />
<input type = "reset" value = "Reset" /><br /><br /></td>
</tr>
</table>
</form>
<table cellspacing="0" cellpadding="10" border="1" bgcolor="lightblue">
<tr>
<td><a href='./index.php'>Homepage</a></td>
<td><a href='../menu.php'>Return to Portal</a></td>
</tr>
</table>
</body>
</html>
Any help will be much appreciated! 🙂
MOD EDIT: Please use the board's [noparse]
[/noparse] bbcode tags when posted PHP code.