I am curious to know if there is a faster, better, and more efficient way to accomplish this program that acquires employee information:
<?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';
}
?>
PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION
will do exactly the same as wrapping your code in try catch. you can verify by removing them all together, a wrong query will still echo the same messages you echo in your catch part, but without the need of writing all those extra lines. \$\endgroup\$