Advertisement
Gayngel

Reroll_Lottery_Database_Main

Apr 5th, 2023 (edited)
1,652
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.02 KB | None | 0 0
  1. <?php
  2.  $host = "localhost";
  3.  
  4. //  put in the username and password of the database into these variables between the quotes.
  5.  $username = "";
  6.  $password = "";
  7.  $dbname = "";
  8.  
  9.  
  10.  try {
  11.     $conn = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
  12.     // set the PDO error mode to exception
  13.     $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  14.    
  15.    
  16.    
  17.      if ($_SERVER["REQUEST_METHOD"] == "POST") {    
  18.      $json = file_get_contents('php://input');
  19.      $data = json_decode($json);
  20.      
  21.      $uuid = $data -> uuid;
  22.      $name = $data -> name;
  23.      $rolls = $data -> rolls;
  24.      $health = $data -> health;
  25.      $race = $data -> race;
  26.      $mpoints = $data -> mod_points;
  27.      $atk_mod = $data -> attack_mod;
  28.      $ev_mod = $data -> evade_mod;
  29.      $st_mod = $data -> stealth_mod;
  30.      $dt_mod = $data -> detect_mod;
  31.      $str_mod = $data -> strength_mod;
  32.      $fin = $data -> finalize;
  33.      $cmd = $data -> cmd;
  34.      
  35.      
  36.      
  37.      $date = date_create('now', timezone_open('America/Los_Angeles'));
  38.      $date = date_format($date, 'Y-m-d H:i:s');
  39.      
  40.      
  41.      
  42.      $datetime = new DateTime();
  43.      $timezone = new DateTimeZone('America/Los_Angeles');
  44.      $datetime->setTimezone($timezone);
  45.      $datetime = $datetime->format('YYYY-MM-DD hh:mm:ss.mmm'); // for example
  46.      
  47.     if($cmd === "POST"){
  48.        
  49.       $chk =  "SELECT * FROM lottery_db WHERE uuid =  '".$uuid."'";
  50.  
  51.       $result = $conn -> query($chk);
  52.      
  53.       if ($result -> rowCount() > 0)
  54.       {
  55.         // 'Found!'
  56.        
  57.     $stmt = $conn->prepare("SELECT `uuid`,`wins`,`health`, `race`, `mod_points`, `evade_mod`, `attack_mod`, `stealth_mod`, `detect_mod`, `strength_mod`, finalize  FROM `lottery_db` WHERE `uuid` = '".$uuid."'");
  58.  
  59.    
  60.     $stmt->execute();
  61.     $rresult = $stmt->fetchALL(\PDO::FETCH_ASSOC); // when getting multiple rows use fetchALL
  62.     echo "Found|".json_encode($rresult);
  63.        
  64.        
  65.        
  66.       }
  67.    
  68.     else
  69.     {
  70.         // not found
  71.          echo "Not Found";
  72.     $sql = "INSERT INTO lottery_db (uuid,name,timestamp,wins, health,race, mod_points,attack_mod,evade_mod,stealth_mod,detect_mod,strength_mod,finalize) VALUES ('".$uuid."','".$name."','".$date."','".$rolls."', '".$health."','".$race."','".$mpoints."','".$atk_mod."','".$ev_mod."','".$st_mod."','".$dt_mod."','".$str_mod."','".$fin."')";
  73.      $conn -> query($sql);
  74.    
  75.     }
  76.        
  77.    
  78.     } // cmd = post
  79.    
  80.     else if($cmd === "UPDATE"){
  81.        
  82.         $sql = "UPDATE lottery_db SET `timestamp` = '".$date."', `health` = '".$health."', `race` = '".$race."', `mod_points` = '".$mpoints."',`attack_mod` = '".$atk_mod."',`evade_mod` = '".$ev_mod."',`stealth_mod` = '".$st_mod."',`detect_mod` = '".$dt_mod."',`strength_mod` = '".$str_mod."',`finalize` = '".$fin."' WHERE `uuid` = '".$uuid."'";  
  83.      $conn -> query($sql);
  84.      
  85.       $stmt = $conn->prepare("SELECT `wins`  FROM `lottery_db` WHERE `uuid` = '".$uuid."'");
  86.  
  87.    
  88.     $stmt->execute();
  89.     $rresult = $stmt->fetchALL(\PDO::FETCH_ASSOC); // when getting multiple rows use fetchALL
  90.     echo "Updated|".json_encode($rresult);
  91.        
  92.     }
  93.    
  94.     else if($cmd === "ROLLS_UPDATE"){
  95.        
  96.         $sql = "UPDATE lottery_db SET `wins` = '".$rolls."' WHERE `uuid` = '".$uuid."'";  
  97.      $conn -> query($sql);
  98.      
  99.       $stmt = $conn->prepare("SELECT `wins`  FROM `lottery_db` WHERE `uuid` = '".$uuid."'");
  100.  
  101.    
  102.     $stmt->execute();
  103.     $rresult = $stmt->fetchALL(\PDO::FETCH_ASSOC); // when getting multiple rows use fetchALL
  104.     echo "Rolls_Updated|".json_encode($rresult);
  105.        
  106.     }
  107.    
  108.     else if($cmd === "RESET"){
  109.        
  110.     $sql = "DELETE FROM lottery_db WHERE `uuid` = '".$uuid."'";  
  111.      $conn -> query($sql);
  112.       echo "database_reset";
  113.      
  114.      
  115.        
  116.     }
  117.    
  118.     else
  119.     {
  120.    
  121.     //$query = $conn->prepare("SELECT `uuid` FROM `lottery_db` WHERE `uuid` = '".$uuid."'");
  122.     //$query->execute();    
  123.     //$result = $query->fetch(\PDO::FETCH_ASSOC);
  124.     //echo json_encode($result);
  125.    
  126.     //echo "\n\n";
  127.    
  128.     //$rand = $conn->prepare("SELECT `uuid` FROM `lottery_db` ORDER BY RAND() LIMIT 3");
  129.    // $rand->execute();
  130.    // $rresult = $rand->fetchALL(\PDO::FETCH_ASSOC); // when getting multiple rows use fetchALL
  131.    // echo json_encode($rresult);
  132.    
  133.    // echo "\n\n";
  134.    
  135.    // $rand = $conn->prepare("SELECT `uuid`, `timestamp` FROM `lottery_db` WHERE TIMESTAMPDIFF(day,`timestamp`,'".$date."' ) < 7  ORDER BY RAND() LIMIT 3");
  136.    // $rand->execute();
  137.    // $rresult = $rand->fetchALL(\PDO::FETCH_ASSOC); // when getting multiple rows use fetchALL
  138.    // echo json_encode($rresult);
  139.    
  140.    // $incr = $conn->prepare("
  141.    // UPDATE `lottery_db`
  142.    // SET `wins` = `wins` + 1
  143.    // WHERE TIMESTAMPDIFF(day,`timestamp`,'".$date."' ) < 7
  144.    // ORDER BY RAND()
  145.    // LIMIT 3");
  146.    //  $incr->execute();
  147.      
  148.     }
  149.    
  150.      }
  151.    
  152.    
  153.     } catch(PDOException $e) {    
  154.     echo "Connection failed: " . $e->getMessage();
  155.     }
  156.  
  157.  
  158.  
  159.      
  160. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement