0

So I changed my script to use the LOAD DATA LOCAL INFILE since it is supposed to me much faster. The problem is I can't get my dates to populate properly.

I have this now. (I've tried 50 different things)

if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
    echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
    echo $target_file;

    // Get file for processing
    $sql = "
            LOAD DATA LOCAL INFILE \"C:/GIT/Production/csv/$target_file\"
            INTO TABLE exp_subs
            FIELDS TERMINATED BY ','
            OPTIONALLY ENCLOSED BY '\"'
            LINES TERMINATED BY '\n'
            (id,site_id,hash,member_id,card_id,coupon_id,plan_id,voucher_id,entry_id,recurring_interval,recurring_interval_count,amount,total_amount,total_cycles,status,renew,trial_starts_at,trial_ends_at,retry_count,retry_date,shipping_name,shipping_address1,shipping_address2,shipping_address3,shipping_city,shipping_state,shipping_zip,shipping_country,notes,test_mode,created_by,updated_by,canceled_by,next_billing_at,last_billed_at,@var1,@var2,@var3,@var4,@var5,@var6)
            SET ended_at = STR_TO_DATE(@var1, '%Y-%m-%d %H:%i:%S'),
            canceled_at = STR_TO_DATE(@var2, '%Y-%m-%d %H:%i:%S'),
            expires_at = STR_TO_DATE(@var3, '%Y-%m-%d %H:%i:%S'),
            created_at = STR_TO_DATE(@var4, '%Y-%m-%d %H:%i:%S'),
            updated_at = STR_TO_DATE(@var5, '%Y-%m-%d %H:%i:%S'),
            deleted_at = STR_TO_DATE(@var6, '%Y-%m-%d %H:%i:%S')
        ";

        mysql_query($sql) or die(mysql_error());
} else {
    echo "Sorry, there was an error uploading your file.";
}

This pulls everything in except the dates. The dates are still being brought in as '0000-00-00 00:00:00'. This is the same thing that happens if I use the simpler version like this:

if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
    echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
    echo $target_file;

    // Get file for processing
    $sql = "
        LOAD DATA LOCAL INFILE \"C:/GIT/Production/csv/$target_file\"
        INTO TABLE exp_subs
        FIELDS TERMINATED BY ','
        OPTIONALLY ENCLOSED BY '\"'
        LINES TERMINATED BY '\n'
    ";

    mysql_query($sql) or die(mysql_error());
} else {
    echo "Sorry, there was an error uploading your file.";
}

The CSV file keeps the dates in this format: "4/21/2016 7:00"

How do I convert them from the file uploaded to the way I need them?

8
  • I'm afraid that didn't help me out. Maybe I'm not following, I understand how to change the date in PHP. I don't know how to change the date within the file data since it is being pulled into the SQL statement straight from the CSV and the CSV file does not allow the formatting required. Commented Jul 23, 2015 at 15:10
  • Did you get that solution working? Commented Jul 23, 2015 at 20:14
  • That would be great. Thanks Commented Jul 23, 2015 at 20:28
  • Let us continue this discussion in chat. Commented Jul 23, 2015 at 20:58
  • @shehary Thanks for working on this with me yesterday. I will post what ended up working for me. It is a bit different that where we were on things, but this goes through and allows for the removal of all empty rows too. I will post the final answer for you to view here. Commented Jul 24, 2015 at 19:01

1 Answer 1

1

I hope this helps someone else down the road...

This is what I ended up with which worked for me. There may be a much easier way, but I was not able to get another way working properly.

Essentially, I had to create a row variable to skip the first row of the CSV file which was the header, then, since I had multiple dates being sent in, I had to break out each column or field in the CSV so that I could grab the fields passed in as dates and create a date field from them, convert them to the required format, then overwrite the variable before adding it to the SQL INSERT statement.

Lastly, I kept ending up with thousands of lines being inserted. The CSV file was seeing a lot of blank (,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,) rows and was adding them as well so I ended up putting in a check for blanks and ending the while loop if it was blank.

<?php
    include "connection.php"; //Connect to Database

    //Upload File
    if (isset($_POST['submit'])) {
        if (is_uploaded_file($_FILES['filename']['tmp_name'])) {
            echo "<h1>" . "File ". $_FILES['filename']['name'] ." uploaded successfully." . "</h1>";
        }

        $csvRow = 1;

        //Import uploaded file to Database
        $csvHandle = fopen($_FILES['filename']['tmp_name'], "r");

        $csvKey = 0; // Set the parent array key to 0

        while (($csvData = fgetcsv($csvHandle)) !== FALSE) {

            if($csvRow == 1){
                $csvRow++;
                continue;
            }

            $c = count($csvData); // Count the total keys in each row

            // 33-40 are dates that need to be converted
            $dateArray = array(33, 34, 35, 36, 37, 38, 39, 40);

            // ## Flag variable ##########
            $empty = true;

            for ($x = 0; $x < $c; $x++) { // Loop through the "columns"

                // ## Test each value ##########
                $empty = $empty && (empty($csvData[$x]));

                if(in_array($x, $dateArray)){
                    $date = date_create($csvData[$x]);
                    $FormattedDate = date_format($date,"Y-m-d H:i:s");
                    $csvData[$x] = $FormattedDate;
                }
            }

            $sql = "" . "INSERT INTO exp_subs(id,site_id,hash,member_id,card_id,coupon_id,plan_id,voucher_id,entry_id,recurring_interval,recurring_interval_count,amount,total_amount,total_cycles,status,renew,trial_starts_at,trial_ends_at,retry_count,retry_date,shipping_name,shipping_address1,shipping_address2,shipping_address3,shipping_city,shipping_state,shipping_zip,shipping_country,notes,test_mode,created_by,updated_by,canceled_by,next_billing_at,last_billed_at,ended_at,canceled_at,expires_at,created_at,updated_at,deleted_at) VALUES('$csvData[0]','$csvData[1]','$csvData[2]','$csvData[3]','$csvData[4]','$csvData[5]','$csvData[6]','$csvData[7]','$csvData[8]','$csvData[9]','$csvData[10]','$csvData[11]','$csvData[12]','$csvData[13]','$csvData[14]','$csvData[15]','$csvData[16]','$csvData[17]','$csvData[18]','$csvData[19]','$csvData[20]','$csvData[21]','$csvData[22]','$csvData[23]','$csvData[24]','$csvData[25]','$csvData[26]','$csvData[27]','$csvData[28]','$csvData[29]','$csvData[30]','$csvData[31]','$csvData[32]','$csvData[33]','$csvData[34]','$csvData[35]','$csvData[36]','$csvData[37]','$csvData[38]','$csvData[39]','$csvData[40]')";
            $result = mysql_query($sql) or die(mysql_error());

            // ## Stop loop if all empty ##########
            if ($empty) {
                break;
            }
            $csvKey++;
        }

        fclose($csvHandle);

        print "<br />Import done";
    } else {
        print "Upload new csv by browsing to file and clicking on Upload<br />\n";
        print "<form enctype='multipart/form-data' action='upload.php' method='post'>";
        print "File name to import:<br />\n";
        print "<input size='50' type='file' name='filename'><br />\n";
        print "<input type='submit' name='submit' value='Upload'></form>";
    }
?>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.