Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- include_once('class.db.php');
- $db = new db;
- $error = Array();
- try{
- // is there a variable for full name
- if(empty($_GET['fullName'])){
- $error[] = 'Full Name Not Provided.';
- throw new Exception(implode(' ',$error));
- }
- // find a comma
- $comma = strpos($_GET['fullName'],',');
- // if comma, strip it out and put first and last name into an array
- if($comma > 0) {
- $fullName = explode(',',str_replace(' ','',$_GET['fullName']));
- $fullName = array_reverse($fullName);
- }else{
- $fullName = explode(' ',$_GET['fullName']);
- }
- // check for first name
- if(empty($fullName[0])){
- $error[] = 'First Name not Provided.';
- }
- // check for last name
- if(empty($fullName[1])){
- $error[] = 'Last Name not Provided.';
- }
- // count the number of errors and throw an exception
- if(count($error) > 0) {
- throw new Exception(implode(' ',$error));
- }
- //Determine if Person already exists in database
- $sql = "SELECT * FROM whosHere WHERE firstname = '".ucfirst($fullName[0])."' AND lastname = '".ucfirst($fullName[1])."'";
- $result = $db->conn->query($sql);
- if($result->num_rows > 0){
- $row = $result->fetch_object();
- $status .= ($row->present==1) ? "out":"in";
- $sql = "UPDATE whosHere SET time=now(), present=";
- $sql .= ($row->present==1) ? 0:1;
- $sql .= " WHERE firstname = '".ucfirst($fullName[0])."' AND lastname = '".ucfirst($fullName[1])."'";
- $result = $db->conn->query($sql);
- echo json_encode(Array('opt'=>$status, 'result'=>true));
- }else{
- $sql = "INSERT INTO whosHere (firstname, lastname, time, present) VALUES ('".ucfirst($fullName[0])."', '".ucfirst($fullName[1])."',now(),1)";
- $sql = $db->conn->query($sql);
- echo json_encode(Array('opt'=>'in', 'result'=>true));
- }
- }catch (Exception $e){
- echo json_encode(Array('message'=>$e->getMessage(),'result'=>false));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement