Advertisement
rsu18amurray

change.php

Jun 27th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.90 KB | None | 0 0
  1. <?php
  2. include_once('class.db.php');
  3. $db = new db;
  4. $error = Array();
  5.  
  6. try{
  7.     // is there a variable for full name
  8.     if(empty($_GET['fullName'])){
  9.         $error[] = 'Full Name Not Provided.';
  10.         throw new Exception(implode(' ',$error));
  11.     }
  12.     // find a comma
  13.     $comma = strpos($_GET['fullName'],',');
  14.    
  15.     // if comma, strip it out and put first and last name into an array
  16.     if($comma > 0) {
  17.         $fullName = explode(',',str_replace(' ','',$_GET['fullName']));
  18.         $fullName = array_reverse($fullName);
  19.     }else{
  20.         $fullName = explode(' ',$_GET['fullName']);
  21.     }
  22.  
  23.     // check for first name
  24.     if(empty($fullName[0])){
  25.         $error[] = 'First Name not Provided.';
  26.     }
  27.    
  28.     // check for last name
  29.     if(empty($fullName[1])){
  30.         $error[] = 'Last Name not Provided.';
  31.     }
  32.    
  33.     // count the number of errors and throw an exception
  34.     if(count($error) > 0) {
  35.         throw new Exception(implode(' ',$error));
  36.     }
  37.    
  38.     //Determine if Person already exists in database
  39.     $sql = "SELECT * FROM whosHere WHERE firstname = '".ucfirst($fullName[0])."' AND lastname = '".ucfirst($fullName[1])."'";
  40.     $result = $db->conn->query($sql);
  41.    
  42.     if($result->num_rows > 0){
  43.         $row = $result->fetch_object();
  44.         $status .= ($row->present==1) ? "out":"in";
  45.        
  46.         $sql = "UPDATE whosHere SET time=now(), present=";
  47.         $sql .= ($row->present==1) ? 0:1;
  48.         $sql .= " WHERE firstname = '".ucfirst($fullName[0])."' AND lastname = '".ucfirst($fullName[1])."'";
  49.        
  50.         $result = $db->conn->query($sql);
  51.        
  52.         echo json_encode(Array('opt'=>$status, 'result'=>true));
  53.     }else{
  54.         $sql = "INSERT INTO whosHere (firstname, lastname, time, present) VALUES ('".ucfirst($fullName[0])."', '".ucfirst($fullName[1])."',now(),1)";
  55.         $sql = $db->conn->query($sql);
  56.        
  57.         echo json_encode(Array('opt'=>'in', 'result'=>true));
  58.        
  59.     }  
  60. }catch (Exception $e){
  61.     echo json_encode(Array('message'=>$e->getMessage(),'result'=>false));
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement