Jump to content

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

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.