<?php

require "db.php";
$db = connectDb();
$db2 = connectUserDb();
if (isset($_GET['originating_officer'])) {
    $division = $_GET['originating_officer'];
    $n = 0;
    $sql = "select * from originating_officer where division='" . $division . "' limit 1";
    $rs = $db->query($sql);
    $nm = $rs->num_rows;
    for ($i = 0; $i < $nm; $i++) {
        $row = $rs->fetch_assoc();
        $data["officer"][$n]["id"] = $row['id'];
        $data["officer"][$n]["name"] = $row['name'];
        $data["officer"][$n]["division"] = $row['division'];
        $n++;
    }
    $sql2 = "select * from users where deptCode='" . $division . "' order by lastName";
    $rs2 = $db2->query($sql2);
    $nm2 = $rs2->num_rows;
    for ($i = 0; $i < $nm2; $i++) {
        $row2 = $rs2->fetch_assoc();
        $data["officer"][$n]["id"] = "OTHER";
        $data["officer"][$n]["name"] = $row2['firstName'] . " " . $row2['lastName'];
        $data["officer"][$n]["division"] = $division;
        $n++;
    }
    $data["record_count"] = $n;
    $data["division"] = $division;
    echo json_encode($data);
<?php

session_start();
require "db.php";
if (isset($_POST['username'])) {
    $db = connectUserDb();
    $db2 = connectDb();
    $sql = "select * from users where username='******'username'] . "' limit 1";
    $rs = $db->query($sql);
    $nm = $rs->num_rows;
    if ($nm > 0) {
        $row = $rs->fetch_assoc();
        if ($_POST['password'] == $row['password']) {
            $_SESSION['username'] = $_POST['username'];
            $_SESSION['name'] = $row['firstName'] . " " . $row['lastName'];
            $_SESSION['passcode'] = $_POST['password'];
            $_SESSION['formcode'] = $_POST['password'];
            $sql2 = "select * from department where department_code='" . $row['deptCode'] . "'";
            $rs2 = $db2->query($sql2);
            $row2 = $rs2->fetch_assoc();
            $_SESSION['division'] = $row2['department_name'];
            $_SESSION['division_code'] = $row['deptCode'];
            header("Location: main_page.php");
        } else {
            header("Location: login.php");
        }
    } else {
        header("Location: login.php");
    }
}
?>