How to update ranking and points for players in MySQLi when uploading new cvs file

Discussion in 'PHP' started by Kurtern84, Jul 18, 2022.

Tags:
  1. Kurtern84

    Kurtern84 New Member

    Joined:
    Jul 17, 2022
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    1
    As the title says. I have a website with table showing ranks of poker tournaments, I have created a db where I uploaded a csv file with the last tournamet. It´s works great.

    But i can´t figure out have to update point and rank, when uploading a new csv. If player one has 6 points and the in the new cvs file the same player has 4 points, i want it to add the new 4 point to the 6 from the last tournament. And then update the rank position.

    Thanks in advance.

    Code:
    <?php
    // include mysql database configuration file
    include_once 'db.php';
     
    if (isset($_POST['submit']))
    {
     
        // Allowed mime types
        $fileMimes = array(
            'text/x-comma-separated-values',
            'text/comma-separated-values',
            'application/octet-stream',
            'application/vnd.ms-excel',
            'application/x-csv',
            'text/x-csv',
            'text/csv',
            'application/csv',
            'application/excel',
            'application/vnd.msexcel',
            'text/plain'
        );
     
        // Validate whether selected file is a CSV file
        if (!empty($_FILES['file']['name']) && in_array($_FILES['file']['type'], $fileMimes))
        {
     
                // Open uploaded CSV file with read-only mode
                $csvFile = fopen($_FILES['file']['tmp_name'], 'r');
     
                // Skip the first line
                fgetcsv($csvFile);
     
                // Parse data from CSV file line by line
                 // Parse data from CSV file line by line
                while (($getData = fgetcsv($csvFile, 10000, ",")) !== FALSE)
                {
                    // Get row data
                    $rank = $getData[0];
                    $name = $getData[1];
                    $points = $getData[2];
     
                    // If user already exists in the database with the same name
                    $query = "SELECT id FROM ranking WHERE name = '" . $getData[1] . "'";
     
                    $check = mysqli_query($conn, $query);
     
                    if ($check->num_rows > 2)
                    {
                        mysqli_query($conn, "UPDATE ranking SET  rank = '" . $rank . "', name = '" . $name . "', points'" . $points . "'");
                    }
                    else
                    {
                         mysqli_query($conn, "INSERT INTO ranking (rank, name, points) VALUES ('" . $rank . "', '" . $name . "', '" . $points . "')");
    
                    }
                }
     
                // Close opened CSV file
                fclose($csvFile);
     
                header("Location: ../leaderboard.php");
            
        }
        else
        {
            echo "Please select valid file";
        }
    }
    
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice