Jump to content


Import csv file to database on upload

csv database import insert excel function upload

  • Please log in to reply
No replies to this topic

#1 shibbolith

shibbolith

    Newbie

  • Members
  • Pip
  • 1 posts

Posted 22 March 2015 - 04:16 PM

I'm looking for an example of how to import the contents of a .csv file to my database when I upload it.

 

My upload function works (will save the file to the appropriate folder and place the url/path in the database), but am struggling with code to import the file to a data-table in my database.

 

I found a separate function that could accomplish this, but have not been able to use it in the CI / Grocery CRUD model I've set up.

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

    //Import uploaded file to Database

    $handle = fopen($_FILES['filename']['tmp_name'], "r");

    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {

        $import="INSERT into contacts_short(contact_first,contact_last,contact_email) values('$data[0]','$data[1]','$data[2]')";

        mysql_query($import) or die(mysql_error());

    }

    fclose($handle);

I'm relatively new to Grocery CRUD but have been impressed with it so far.  Any help would be appreciated.







Also tagged with one or more of these keywords: csv, database, import, insert, excel, function, upload

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users