Jump to content

Coding Suggestions


stemp

Recommended Posts

I need to build an application in which a user uploads a CSV file through a web form and the application dumps the data it into a mySQL DB.

 

The mySQL DB will be set up the same as the CSV file as far as number of fields and data types.

 

I guess I would just need to have the application format the CSV into the format that mySQL reads right?

 

Any suggestions would be appreciated.

 

 

Link to comment
https://forums.phpfreaks.com/topic/62688-coding-suggestions/
Share on other sites

I'd suggest using fgetcsv() to read the file into an array - each line is an element. Then separate the fields into elements using explode. Then after validating the information for each field, run a query to insert the data.

 

<?php

$handle = fopen("test.csv", "r");

while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
    $name = $data[0];
    $address = $data[1];
    $phone = $data[2];

  //Validate the date

  //Run query to insert record
}

close($handle);
?> 

 

Also, depending how many records there will be you could increase the efficiencly by using the loop to create a single insert query instead of running multiple queries.

Link to comment
https://forums.phpfreaks.com/topic/62688-coding-suggestions/#findComment-312070
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.