Skip to main content
added 32 characters in body; edited title
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

Is there a more efficient way to do this Acquiring employee information

I am curious to know if there is a faster, better, and more efficient way to accomplish what I have in the code belowthis program that acquires employee information:

Is there a more efficient way to do this

I am curious if there is a faster better more efficient way to accomplish what I have in the code below:

Acquiring employee information

I am curious to know if there is a faster, better, and more efficient way to accomplish this program that acquires employee information:

Tweeted twitter.com/#!/StackCodeReview/status/290362481765937153
Source Link

Is there a more efficient way to do this

I am curious if there is a faster better more efficient way to accomplish what I have in the code below:

    <?php
session_start();
//require 'functions.php';
//require 'DB.php';
$employeeID = $_SESSION['employeeID'];
$clockIn = $_GET['clockIn'];
$clockOut = $_GET['clockOut'];
$timeToday = date("g:i a");
$dateToday = date("m/d/y");
$jobDescription = $_GET['jobDesc'];
$equipType = $_GET['equipTypeRan'];
$unitNumber = $_GET['unitNumber'];
$unitHours = $_GET['unitHours'];
if (isset($clockIn)) {
    
    echo "You clocked in at: " . $timeToday . " on " . $dateToday;
    
    try {
    $conn = new PDO('mysql:host=localhost;dbname=timecard', 'username', 'password');
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $stmt = $conn->prepare('INSERT INTO timeRecords (employeeID, date, timeIn) VALUES (:employeeID, :dateToday, :timeIn)');

    $stmt->execute(array(':employeeID' => $_SESSION['employeeID'], ':dateToday' => $dateToday, ':timeIn' => $timeToday));

    } catch(PDOException $e){
        echo'ERROR: ' . $e->getMessage();
    }
    $conn = null;
    

} else if (isset($clockOut)) {
    
        try {
    $conn = new PDO('mysql:host=localhost;dbname=timecard', 'username', 'password');
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $stmt = $conn->prepare('UPDATE `timeRecords` SET `timeOut`= :timeOut WHERE `date`= :dateToday AND `employeeID`= :employeeID');

    $stmt->execute(array(':employeeID' => $_SESSION['employeeID'],':dateToday' => $dateToday, ':timeOut' => $timeToday));

    $stmt = $conn->prepare('SELECT `timeIn` FROM `timeRecords` WHERE `date`= :dateToday AND `employeeID` = :employeeID');
    $stmt->execute(array(':employeeID' => $_SESSION['employeeID'], ':dateToday' => $dateToday));
    $stmt->setFetchMode(PDO::FETCH_BOTH);
    $results = $stmt->fetch();
    $timeInDB = $results[0];

    } catch(PDOException $e){
        echo'ERROR: ' . $e->getMessage();
    }

    $time_one = new DateTime($timeInDB);
    $time_two = new DateTime($timeToday);
    $difference = $time_one->diff($time_two);
    
    echo "You clocked in at: " . $timeInDB . "<br>";
    echo "You clocked out at: " . $timeToday . "<br>";
    
    echo $difference->format('Total working time %h hours %i minutes');
    
}else {
    
try{
    $conn = new PDO('mysql:host=localhost;dbname=timecard', 'username', 'password');
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $stmt = $conn->prepare("SELECT COUNT(*) FROM timeRecords WHERE `timeOut` IS NULL AND `employeeID`= :employeeID AND `date`= :dateToday");

    $stmt->execute(array(':employeeID' => $employeeID, ':dateToday' => $dateToday));
    } catch(PDOException $e){
        echo'ERROR: ' . $e->getMessage();
    }
    
if($stmt->fetchColumn() > 0){
    
    try{
    $conn = new PDO('mysql:host=localhost;dbname=timecard', 'username', 'password');
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $stmt = $conn->prepare('UPDATE `timeRecords` SET `jobDescription`= :jobDescription, `equipType`= :equipType, `unitNumber`= :unitNumber, `unitHours`= :unitHours, 
                            `timeOut`= :timeOut WHERE `employeeID`= :employeeID AND `date`= :dateToday AND `timeOut` IS NULL');

    $stmt->execute(array(':employeeID' => $employeeID, ':timeOut' => $timeToday, ':dateToday' => $dateToday, ':jobDescription' => $jobDescription,
                         ':equipType' => $equipType, ':unitNumber' => $unitNumber, ':unitHours' => $unitHours));
    
    $stmt = $conn->prepare('INSERT INTO timeRecords (employeeID, date, timeIn) VALUES (:employeeID, :dateToday, :timeIn)');
    $stmt ->execute(array(':employeeID' => $employeeID, ':dateToday'=> $dateToday, ':timeIn' => $timeToday));
    } catch(PDOException $e){
        echo'ERROR: ' . $e->getMessage();
    }
    
    echo "There was an update";
} else {
    echo "There was no Match";
}

    require 'summary.php';

}

?>