Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- if(isset($_POST["email"]) && isset($_POST["username"]) && isset($_POST["score"])){
- $errors = array();
- $email = $_POST["email"];
- $username = $_POST["username"];
- $submitted_score = floatval($_POST["score"]);
- $user_id = -1;
- $current_highscore = 1000;
- //Connect to database
- require dirname(__FILE__) . '/database.php';
- //Check if the user already registered, retrieve its user_id and score value (if exist)
- if ($stmt = $mysqli_conection->prepare("SELECT u.user_id,
- (SELECT user_score FROM sc_user_scores WHERE user_id = u.user_id LIMIT 1) as user_score
- FROM sc_users u WHERE u.email = ? AND u.username = ? LIMIT 1")) {
- /* bind parameters for markers */
- $stmt->bind_param('ss', $email, $username);
- /* execute query */
- if($stmt->execute()){
- /* store result */
- $stmt->store_result();
- if($stmt->num_rows > 0){
- /* bind result variables */
- $stmt->bind_result($user_id_tmp, $score_tmp);
- /* fetch value */
- $stmt->fetch();
- $user_id = $user_id_tmp;
- $current_highscore = $score_tmp;
- }else{
- $errors[] = "User not found.";
- }
- /* close statement */
- $stmt->close();
- }else{
- $errors[] = "Something went wrong, please try again.";
- }
- }else{
- $errors[] = "Something went wrong, please try again.";
- }
- //Submit new score
- if(count($errors) == 0){
- if(is_null($current_highscore)){
- if(is_null($current_highscore)){
- //Insert new record
- if ($stmt = $mysqli_conection->prepare("INSERT INTO sc_user_scores (user_id, user_score) VALUES(?, ?)")) {
- /* bind parameters for markers */
- $stmt->bind_param('ii', $user_id, $submitted_score);
- /* execute query */
- if($stmt->execute()){
- /* close statement */
- $stmt->close();
- }else{
- $errors[] = "Something went wrong, please try again.";
- }
- }else{
- $errors[] = "Something went wrong, please try again.";
- }
- }else{
- //Update existing record
- if ($stmt = $mysqli_conection->prepare("UPDATE sc_user_scores SET user_score = ? WHERE user_id = ? LIMIT 1")) {
- /* bind parameters for markers */
- $stmt->bind_param('ii', $submitted_score, $user_id);
- /* execute query */
- if($stmt->execute()){
- /* close statement */
- $stmt->close();
- }else{
- $errors[] = "Something went wrong, please try again.";
- }
- }else{
- $errors[] = "Something went wrong, please try again.";
- }
- }
- }else{
- $errors[] = "Submitted score is lower than the current highscore, skipping...";
- }
- }
- if(count($errors) > 0){
- echo $errors[0];
- }else{
- echo "Success";
- }
- }else{
- echo "Missing data";
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement