Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Upload CSV into database using php

Tags:

php

mysql

csv

I am working on a little project of mine and I came to the conclusion that being able to automatically upload a excel sheet to my database would be very, very useful,the problem is that I don´t have an idea of where to start, I have researched a bit and decieded to use a CSV file created from a excel sheet to upload the data into the table of my DB.

Most of the examples I have seem look like a mess with the PHP code into the html instead of dividing the logic in different files like what I have been doing in this last 2 months.

What I have right now is the upload form in html:

       <form enctype="multipart/form-data" method="post" id="uploadForm">
          <input name="filesfiles" id="upload" type="file" accept=".csv" class="left" />
          <input type="submit" value="Cargar" />
       </form>

And a small sample of how the CSV file looks in text:

Cedula;Nombre;Apellido1;Apellido2;Correo;IdRol;Estado
1657890;Dominico;Scarlatti;Viera;[email protected];2;0
5657890;Franz;Listz;Linerman;[email protected];3;0

Or in some other excel versions:

Cedula,Nombre,Primer Apellido,Segundo Apellido,Correo,IDRol,Estado
126548791,Franz ,Ritter ,von Liszt,[email protected],3,0
174657109,Sofia ,Asgatovna ,Gubaidulina ,[email protected],3,0

The first row is the name of the columns (which should be ignored when adding the info) of the table I want to upload the file into.

The problem is that I don´t know how to link the upload file once the submit button is clicked to a PHP code in my includes that inserts the CSV into the table.

Thanks a lot in advance

EDIT:

JSFiddle of the upload form

EDIT4:

I am a stroke of pure genius and skill Maduka was able to help me solve this behemoth of problem. I can't thank him enough, the following is the code used in hopes that it may serve someone someday and save them the grief of failure.

<?php

error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED & ~E_WARNING & ~E_STRICT);

mysql_connect('localhost', 'root', '');
mysql_select_db("proyecto") or die(mysql_error());

if (isset($_FILES['csvupload'])) {
    $errors = array();
    $allowed_ext = array('.csv');

    $file_name = $_FILES['csvupload']['name'];
    $file_ext = strtolower(end(explode('.', $file_name)));
    $file_size = $_FILES['csvupload']['size'];
    $file_tmp = $_FILES['csvupload']['tmp_name'];

    if (in_array($allowed_ext) === false) {
        $errors[] = 'La extensión del archivo no es valida.';
    }
    if ($file_size > 10485760) {
        $errors[] = 'El archivo sobrepasa el limite de 10MB';
    }
    if (empty($errors)) {

        $handle = fopen($file_tmp, "r");

        while (!feof($handle)) {
    $value = (fgetcsv($handle, 0, ','));
    if ($i > 0) {
        if ($value[0] != '') {
            $inserts[] = "('" . mysql_real_escape_string($value[0]) . "','"
                    . mysql_real_escape_string($value["1"]) . "','"
                    . mysql_real_escape_string($value["2"]) . "','"
                    . mysql_real_escape_string($value["3"]) . "','"
                    . mysql_real_escape_string($value["4"]) . "','"
                    . mysql_real_escape_string($value["5"]) . "','"
                    . mysql_real_escape_string($value["6"]) . "')";
        }
    } elseif ($i == 0) {
        $fields = $value;
    }
    $i++;
}

mysql_query("INSERT INTO `usuarios` (`cedula`,`nombre`,`apellido1`,`apellido2`,`correo`,`idRol`,`estado`) VALUES " . implode(",", $inserts));
        fclose($handle);
        if ($sq1) {
            echo '¡Los usuarios han sido agregados exitosamente!';
        }
    }
}
?>
like image 832
Code Grasshopper Avatar asked Nov 01 '22 01:11

Code Grasshopper


1 Answers

Here is the basic code which you need to do your task,

$file = fopen($_FILES['csvUpload']['tmp_name'], "r");
$i = 0;
while (!feof($file)) {
    $value = (fgetcsv($file, 0, ';'));
    if ($i > 0) {
        if ($value[0] != '') {
            $inserts[] = "(" . $value[0] . ","
                    . $value["1"] . ","
                    . $value["2"] . ","
                    . $value["3"] . ","
                    . $value["4"] . ","
                    . $value["5"] . ","
                    . $value["6"] . ")";
        }
    } elseif ($i == 0) {
        $fields = $value;
    }
    $i++;
}

mysql_query("INSERT INTO `MyTable` (`" . $fields[0] . "`,`" . $fields[1] . "`,`" . $fields[2] . "`,`" . $fields[3] . "`,`" . $fields[4] . "`,`" . $fields[5] . "`) VALUES " . implode(",", $inserts));

fclose($file);

You have to implement validation, check file type and size limit. Then insert your data to the table. I have use MySQL bulk insert to handle large amount of data. Hope this helps!

EDIT 1:

Please replace your code with this code and see if it is working correctly.

<?php
error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED & ~E_WARNING);

mysql_connect('localhost', 'root', '');
mysql_select_db("proyecto") or die(mysql_error());

if (isset($_FILES['csvUpload'])) {
    $errors = array();
    $allowed_ext = array('.csv');

    $file_name = $_FILES['csvUpload']['name'];
    $file_ext = strtolower(end(explode('.', $file_name)));
    $file_size = $_FILES['csvUpload']['size'];
    $file_tmp = $_FILES['csvUpload']['tmp_name'];

    if (in_array($allowed_ext) === false) {
        $errors[] = 'La extensión del archivo no es valida.';
    }
    if ($file_size > 10485760) {
        $errors[] = 'El archivo sobrepasa el limite de 10MB';
    }
    if (empty($errors)) {


        $handle = fopen($file_tmp, "r");
        while (($fileop = fgetcsv($handle, ";") && fgetcsv($handle, ",")) !== false) {
            $cedula = mysql_real_escape_string($fileop[0]);
            $nombre = mysql_real_escape_string($fileop[2]);
            $apellido1 = mysql_real_escape_string($fileop[3]);
            $apellido2 = mysql_real_escape_string($fileop[4]);
            $correo = mysql_real_escape_string($fileop[5]);
            $idRol = mysql_real_escape_string($fileop[6]);
            $estado = mysql_real_escape_string($fileop[9]);


            $sq1 = mysql_query("INSERT INTO `usuarios` (cedula,nombre,apellido1,apellido2,correo,idRol,estado) VALUES ('$cedula','$nombre','$apellido1','$apellido2','$correo','$idRol','$estado')");
        }
        fclose($handle);
        if ($sq1) {
            echo '¡Los usuarios han sido agregados exitosamente!';
        }
    }
}
?>



<form enctype="multipart/form-data" method="post" id="uploadForm">
    <input name="csvUpload" id="upload" type="file" accept=".csv" class="left" />
    <input type="submit" value="¡Cargar!" />
</form>
like image 53
Maduka Jayalath Avatar answered Nov 10 '22 06:11

Maduka Jayalath