How to use AJAX within a .PHP when importing data from a .CSV

0
 <!DOCTYPE html>
   <html>
     <head>
   <title></title>
     <script type="text/javascript" src="header.php"></script>
     </head>
    <body>
     <h1>Importando archivo CSV</h1>
    <form action='importar.php' method='post' enctype="multipart/form-data">
    Importar Archivo : <input type='file' name='sel_file' size='20'>
        <input type='submit' name='submit' " value='submit'>
     </form>
    </body>
   </html>

After creating the button to import the data of an excel (.csv) I have the php import.php, in which is where I make the call to load the values

<?php

//conexiones, conexiones everywhere
 ini_set('display_errors', 1);
error_reporting(E_ALL);
$mysql= new mysqli("localhost","root","","inci");

  if ($mysql->connect_errno) {
echo "no se pudo acceder a la base de datos";
  }
 else{
if(isset($_POST['submit']))
{
    //Aquí es donde seleccionamos nuestro csv
     $fname = $_FILES['sel_file']['name'];
     echo 'Cargando nombre del archivo: '.$fname.' ';
     $chk_ext = explode(".",$fname);

     if(strtolower(end($chk_ext)) == "csv")
     {
         //si es correcto, entonces damos permisos de lectura para subir
         $filename = $_FILES['sel_file']['tmp_name'];
         $handle = fopen($filename, "r");

         while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
         {
           //Insertamos los datos con los valores...

 $id=$data[0];
 $incidencia=$data[1];
 $resumen=$data[2];

  //tratar de usar ajax aca

  if ($resumen!="RESUMEN") {
 $query=$mysql->query("INSERT INTO registro(id,incidencia,resumen) VALUES ('$id','$incidencia','$resumen')");

         }
        }
         //cerramos la lectura del archivo "abrir archivo" con un "cerrar archivo"
         fclose($handle);
         echo "<br>Importación exitosa!";
         include 'nucleo.php';

     }
     else
     {
        //si aparece esto es posible que el archivo no tenga el formato adecuado, inclusive cuando es cvs, revisarlo para             //ver si esta separado por " , "
         echo "<br>Archivo invalido!";
     }   
}
}

?>

As you can see there are 3 columns that I bring from my pc, but what happens is that these values will fulfill another function ... since not only will I save them in a mysql, but my mysql has 5 columns and what happens is that these data are going to interact, one x one ... so that my interaction is seen create the nucleus.php (which is included in this code) in which performs the function I want.

     <?php
     $mysqlx = new mysqli("localhost","root","","inci");   
     $cant0= $mysqlx->query("SELECT * FROM registro");
     $row_cant0=$cant0->num_rows;
     for ($w=1; $w<=$row_cant0 ; $w++) { 
     $mysqli0 = new mysqli("localhost","root","","inci");
     $rus=$mysqli0->query("SELECT * FROM registro WHERE id='$w'");
     while($h7 = $rus->fetch_object()){
       $incidencia=$h7->incidencia;
     $resumen=$h7->resumen;

     }


 $mysqli = new mysqli("localhost","root","","mantenimiento");
 $resumen2=strtoupper($resumen);
 $vres=0;
 while ( $vres<strlen($resumen2) && strpos($resumen2, ' ',$vres+1)!=null) {
   $x= strpos($resumen2, ' ',$vres);
   $y=$x+1;
   $z=strpos($resumen2, ' ',$y);
   $p=$z-$y;
   $vres=$z;
   $abc=substr($resumen2, $y, $p);
   //$cantidad=$mysqli->query("SELECT COUNT * FROM sites ");
   if (strlen($abc)>=6 && strlen($abc)<=7 && substr($resumen2, $y,2)=="AC" OR substr($resumen2, $y,2)=="LI"){
    if (strlen($abc)>6 && ctype_alpha($abc[2])) {
              $aa=substr($resumen2, $y,2);
              $ab=substr($resumen2, $y+3,4);
              $abc=$aa.$ab;
            }elseif (strlen($abc)>6) {
              $abc=substr($resumen2, $y, $p-1);
            }
            $cant= $mysqli->query("SELECT * FROM sites");
            $row_cant=$cant->num_rows;    
      for ($i=1; $i<$row_cant; $i++) { 
          $rs=$mysqli->query("SELECT * FROM sites WHERE id_site='$i'");
            while($f = $rs->fetch_object()){
              $scelda=$f->id_celda;
              $snom=$f->nombre;
            }
         if ($scelda==$abc) {
             $site=$scelda;
             $c=strpos($snom, '/',0);
             $d=substr($snom, $c+1,strlen($snom)-$c-1);
             $stab=$d;
             $vres=strlen($resumen2);
             break;
         }else{
           $site="";
           $stab="";
         }
       }
   } 
 }

   $mysqli8 = new mysqli("localhost","root","","inci");
    $fila2=$mysqli8->query("UPDATE registro SET sitio='$site' WHERE id='$w'");
      $fila3=$mysqli8->query("UPDATE registro SET nombre='$stab' WHERE id='$w'");


    }

   ?>

This process is done in a good way, the problem is the time it takes to load, one minute x 5 rows, and if it had an excel (.csv) of more values (10000) it would take much longer, it is for So my question would be if there would be any way to use AJAX inside my import.php, so that when each row is loaded, the parameters are passed and automatically updated in the mysql, since that is what the AJAX does.

    
asked by kev 15.05.2018 в 23:06
source

0 answers