Advertisement
rdgorodrigo

Untitled

Oct 27th, 2020
1,007
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.77 KB | None | 0 0
  1. <?php
  2.     if(isset($_POST["email"]) && isset($_POST["username"]) && isset($_POST["score"])){
  3.         $errors = array();
  4.  
  5.         $email = $_POST["email"];
  6.         $username = $_POST["username"];
  7.         $submitted_score = floatval($_POST["score"]);
  8.         $user_id = -1;
  9.         $current_highscore = 1000;
  10.  
  11.         //Connect to database
  12.         require dirname(__FILE__) . '/database.php';
  13.  
  14.         //Check if the user already registered, retrieve its user_id and score value (if exist)
  15.         if ($stmt = $mysqli_conection->prepare("SELECT u.user_id,
  16.             (SELECT user_score FROM sc_user_scores WHERE user_id = u.user_id LIMIT 1) as user_score
  17.             FROM sc_users u WHERE u.email = ? AND u.username = ? LIMIT 1")) {
  18.  
  19.             /* bind parameters for markers */
  20.             $stmt->bind_param('ss', $email, $username);
  21.  
  22.             /* execute query */
  23.             if($stmt->execute()){
  24.  
  25.                 /* store result */
  26.                 $stmt->store_result();
  27.  
  28.                 if($stmt->num_rows > 0){
  29.  
  30.                     /* bind result variables */
  31.                     $stmt->bind_result($user_id_tmp, $score_tmp);
  32.  
  33.                     /* fetch value */
  34.                     $stmt->fetch();
  35.  
  36.                     $user_id = $user_id_tmp;
  37.                     $current_highscore = $score_tmp;
  38.  
  39.                 }else{
  40.                     $errors[] = "User not found.";
  41.                 }
  42.  
  43.                 /* close statement */
  44.                 $stmt->close();
  45.  
  46.             }else{
  47.                 $errors[] = "Something went wrong, please try again.";
  48.             }
  49.         }else{
  50.             $errors[] = "Something went wrong, please try again.";
  51.         }
  52.  
  53.         //Submit new score
  54.         if(count($errors) == 0){
  55.             if(is_null($current_highscore)){
  56.  
  57.                 if(is_null($current_highscore)){
  58.                     //Insert new record
  59.                     if ($stmt = $mysqli_conection->prepare("INSERT INTO sc_user_scores (user_id, user_score) VALUES(?, ?)")) {
  60.  
  61.                         /* bind parameters for markers */
  62.                         $stmt->bind_param('ii', $user_id, $submitted_score);
  63.  
  64.                         /* execute query */
  65.                         if($stmt->execute()){
  66.  
  67.                             /* close statement */
  68.                             $stmt->close();
  69.  
  70.                         }else{
  71.                             $errors[] = "Something went wrong, please try again.";
  72.                         }
  73.                     }else{
  74.                         $errors[] = "Something went wrong, please try again.";
  75.                     }
  76.                 }else{
  77.                     //Update existing record
  78.                     if ($stmt = $mysqli_conection->prepare("UPDATE sc_user_scores SET user_score = ? WHERE user_id = ? LIMIT 1")) {
  79.  
  80.                         /* bind parameters for markers */
  81.                         $stmt->bind_param('ii', $submitted_score, $user_id);
  82.  
  83.                         /* execute query */
  84.                         if($stmt->execute()){
  85.  
  86.                             /* close statement */
  87.                             $stmt->close();
  88.  
  89.                         }else{
  90.                             $errors[] = "Something went wrong, please try again.";
  91.                         }
  92.                     }else{
  93.                         $errors[] = "Something went wrong, please try again.";
  94.                     }
  95.                 }
  96.  
  97.             }else{
  98.                 $errors[] = "Submitted score is lower than the current highscore, skipping...";
  99.             }
  100.         }
  101.  
  102.         if(count($errors) > 0){
  103.             echo $errors[0];
  104.         }else{
  105.             echo "Success";
  106.         }
  107.     }else{
  108.         echo "Missing data";
  109.     }
  110. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement