Advertisement
Guest User

Untitled

a guest
Oct 3rd, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.89 KB | None | 0 0
  1. <?php
  2.  
  3. ini_set('display_errors', 1);
  4. ini_set('display_startup_errors', 1);
  5. error_reporting(E_ALL);
  6.  
  7.  
  8. class Test{
  9.  
  10.     public function __construct(){
  11.         if( $this -> checkAPIKey()){
  12.            
  13.         }
  14.         $this->reserveSeat();
  15.     }
  16.  
  17.     public function connect() {
  18.         // db vars stuff
  19.          $servername = 'localhost';
  20.          $username = 'root';
  21.          $password = '';
  22.          $dbname = "i";
  23.  
  24.         // db connection
  25.         $conn = new mysqli($servername, $username, $password, $dbname);
  26.  
  27.         // check connection
  28.         if ($conn->connect_error) {
  29.             die('Connection failed: ' . $conn->connect_error);
  30.         }
  31.         return $conn;
  32.     }
  33.  
  34.     public function checkAPIKey(){
  35.         if( isset( $_POST['api_key']) && !empty($_POST['api_key']) ) {
  36.             $db = $this -> connect();
  37.             $query = "SELECT * FROM `vh_keys` where `api_key` = '" . $_POST['api_key'] . "'";
  38.             $result = mysqli_query($db, $query);
  39.  
  40.             if (mysqli_num_rows($result) == 0) {
  41.                 die( 'Invalid API key' );
  42.             }
  43.  
  44.             return true;
  45.         }
  46.     }
  47.  
  48.     public function reserveSeat(){
  49.         //check seat_rand
  50.         if( $_POST['seat_rand'] == false ) {
  51.             $db = $this -> connect();
  52.             // get random id
  53.             $query_select = "SELECT `id` FROM `vh_seats` where `user_id` is NULL ORDER BY RAND() LIMIT 1";
  54.             $result = mysqli_query($db, $query_select);
  55.             $row = $result->fetch_assoc();
  56.  
  57.             //insert with randomn id;
  58.             $query_update = "UPDATE `vh_seats` SET `user_id` = '1', `date` ='CURDATE()' WHERE `vh_seats`.`id` = " . $row['id'] . ";";
  59.  
  60.             $result = mysqli_query($db, $query_update);
  61.            
  62.         }
  63.     }
  64.  
  65.  
  66.  
  67.     public function return_seats(){
  68.         $db = $this -> connect();
  69.         $query = "SELECT * FROM `vh_seats` where `user_id` is NULL";
  70.         $result = mysqli_query($db, $query);
  71.  
  72.         if (mysqli_num_rows($result) > 0) {
  73.             while($row = mysqli_fetch_assoc($result)) {
  74.                 $json[] = $row;
  75.             }
  76.         } else {
  77.             echo "0 results";
  78.         }
  79.  
  80.         echo json_encode($json);
  81.  
  82.     }
  83.  
  84.  
  85. }
  86.  
  87. $t = new Test();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement