Jump to content

Recommended Posts

I have built a PHP/MySQL CSV uploader/updater to update scores for high school football teams.

 

When I ran this script on XAMPP with Windows 7 I got a permissions error for the "LOAD DATA INFILE" query. I checked my permissions within PHPMyAdmin and the user had all permissions so that isn't it.

 

I need to fix that issue first. I ended up getting everything uploaded by running the query directly on PHPMyAdmin. However, when I try to run the two upload queries my script timed out.

 

The process I am going for here is to upload the file and import the records into a new table (csv_data). Then I want to run the two update queries to update the game table with the new data in the csv_data table.

 

The reason for the two queries is because sometimes the wrong team is in the wrong column as far as home/visitor goes. I find that using CONCAT() is really slowing things down.

 

So I need to first fix my permissions issue with the "LOAD DATA INFILE" query and then figure out a way to speed up my update queries. Any ideas?

 

<?php
require_once('global.php');
if (array_key_exists('submit', $_POST)) {
    if ($_FILES['csv']['error'] > 0){
        echo "Error: " . $_FILES['csv']['error'] . "</br>" . "You have not selected a file or there is another error."; 
    } else {
        $tmp = $_FILES['csv']['tmp_name'];
    }
    if (!$_FILES['csv']['type'] == 'text/csv'){
        echo "Please select a CSV File";
    } else {
        $location = dirname(__FILE__) . '/csv/' . basename($_FILES['csv']['name']);
        move_uploaded_file($tmp, $location);
    }
    $errors = array();
    // Need to get this query to work properly regarding permissions
    $query = $db->prepare("LOAD DATA INFILE ? 
        INTO TABLE csv_data 
        FIELDS TERMINATED BY ',' 
        LINES TERMINATED BY '\n' 
        (game_week, home_team, home_score, visitor_team, visitor_score)");
    if ($query->execute(array($location))) {
        try {
            // Need to figure out how to use LIKE instead of = and do it quickly
            $query_two = $db->prepare("UPDATE game g, csv_data c, team home_team, team visit_team 
                SET g.game_home_score = c.home_score, 
                    g.game_visitor_score = c.visitor_score, 
                    g.game_complete = 'Y' 
                WHERE g.game_week = c.game_week 
                    AND home_team.team_name LIKE CONCAT('%', c.home_team, '%') 
                    AND home_team.team_id  LIKE CONCAT('%', g.game_home_team, '%') 
                    AND visit_team.team_name  LIKE CONCAT('%', c.visitor_team, '%') 
                    AND visit_team.team_id  LIKE CONCAT('%', g.game_visitor_team, '%')");
            if (!$query_two->execute()) {
                $errors[] = 'The first update query failed.';
            }
            $query_three = $db->prepare("UPDATE game g, csv_data c, team home_team, team visit_team 
                SET g.game_home_score = c.visitor_score, 
                    g.game_visitor_score = c.home_score, 
                    g.game_complete = 'Y' 
                WHERE g.game_week = c.game_week 
                    AND home_team.team_name  LIKE CONCAT('%', c.visitor_team, '%') 
                    AND home_team.team_id  LIKE CONCAT('%', g.game_home_team, '%') 
                    AND visit_team.team_name  LIKE CONCAT('%', c.home_team, '%') 
                    AND visit_team.team_id  LIKE CONCAT('%', g.game_visitor_team, '%')");
            if (!$query_three->execute()) {
                $errors[] = 'The second update query failed.';
            }
        } catch(PDOException $e) {
            echo $e->getMessage();
        }
    } else {
        $errors[] = 'CSV Upload Query Failed.';
    }
}
?>

Link to comment
https://forums.phpfreaks.com/topic/268301-complete-csv-uploadupdater/
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.