Filter with department and date range of attendance

if(isset($_GET[‘from_date’]) && isset($_GET[‘to_date’]))
{
$from_date = $_GET[‘from_date’];
$to_date = $_GET[‘to_date’];

    $query = "SELECT * FROM tbl_attendance INNER JOIN tbl_user ON tbl_user.IDNUMBER = tbl_attendance.IDNUMBER WHERE AttendDate BETWEEN '$from_date' AND '$to_date' ORDER BY LASTNAME DESC" ;
    $query_run = mysqli_query($con, $query);

    if(mysqli_num_rows($query_run) > 0)
    {
        foreach($query_run as $row)
        {
            ?>
            <tr>
                <td><?= $row['IDNUMBER']; ?></td>
                <td><?= $row['FIRSTNAME']; ?></td>
                <td><?= $row['TimeInAM']; ?></td>
                <td><?= $row['TimeOutPM']; ?></td>
                <td><?= $row['AttendDate']; ?></td>
            </tr>
            <?php
        }
    }
    else
    {
        echo "No Record Found";
    }
}

`

Here is my table structure:

tbl_user

IDNumber Name. Department
123001 Person 1 Accounting
231002 Person 2 Accounting
432323 Person 3 Accounting
512321 Person 4 IT Dept.
154352 Person 5 IT Dept.
842652 Person 7 Maintenance

tbl_attendance

IDNumber Attendate TimeIn TimeOut
123001 2022-01-01 08:00:00 17:00:00
231002 2022-01-01 07:40:00 17:10:00
432323 2022-01-01 07:40:00 17:10:00
123001 2022-01-02 07:50:00 16:50:00
231002 2022-01-02 08:00:00 17:00:00
154352 2022-01-03 08:20:00 17:20:00
512321 2022-01-03 08:07:00 17:07:00

You should also have a department table, with id (autoincrement primary index) and name columns. This will result in department ids. You would store the department id in the user table, not the department name. This will result in the fastest queries and the least amount of data storage.

You would query the department table and use the query result to build a department select/option menu in the search form that would submit the selected department id.

You should be using a prepared query, instead of putting values directly into the sql query statement. If it seems like using the mysqli extension is overly complicated when dealing with prepared queries, it is. This would be a good time to switch to the much simpler and better designed PDO extension.

Sponsor our Newsletter | Privacy Policy | Terms of Service