Bulk file import from excel.CSV Notice: Undefined offset: 1

0

I make a massive import form of excel.CSV, I care about all the records but when the process ends it shows me this error:

Notice: Undefined offset: 1 in C: \ xampp \ htdocs \ import_excel.php on line 27

Notice: Undefined offset: 2 in C: \ xampp \ htdocs \ import_excel.php on line 27

Annex the code.

Thanks / Regards.

<?php

if (isset($_POST["enviar"])) {//nos permite recepcionar una variable que si exista y que no sea null
	require_once("conexion_excel.php");
	require_once("functions.php");

	$archivo = $_FILES["archivo"]["name"];
	$archivo_copiado= $_FILES["archivo"]["tmp_name"];
	$archivo_guardado = "copia_".$archivo;

	//echo $archivo."esta en la ruta temporal: " .$archivo_copiado;

	if (copy($archivo_copiado ,$archivo_guardado )) {
		//echo "se copeo correctamente el archivo temporal a nuestra carpeta de trabajo <br/>";
	}else{
		//echo "hubo un error <br/>";
	}
    
    if (file_exists($archivo_guardado)) {
    	 
    	 $fp = fopen($archivo_guardado,"r");//abrir un archivo
         $rows = 0;
         while ($datos = fgetcsv($fp , 1000 , ";")) {
             $rows ++;
            // echo $datos[0] ." ".$datos[1] ." ".$datos[2]." ".$datos[3] ."<br/>";
         	if ($rows > 1) {
         		$resultado = insertar_datos($datos[0],$datos[1],$datos[2]);
         	    if($resultado){
         		  //echo "se inserto los datos correctamente<br/>";
            	    } else {
            		//echo "no se inserto <br/>";
            	    }
         	}
         }

    } else {
    	echo "No existe el archivo copiado <br/>";
    }

}


?>
    
asked by Carlos 15.03.2018 в 11:52
source

2 answers

1

The problem seems to be here:

  

$ data [0], $ data [1], $ data [2]

If any of these sub-indices are not started, this Notice comes out.

There are several solutions but it depends on the version of php you use:

for 5.x and earlier: you have to ask if the index is created and if it is not, pass a default value.

isset($datos[0])?$datos[0]:'dato por defecto'

for 7.x can you use the ?? (Null Coalesce Operator).

$datos[0] ?? 'valor por defecto'

With this you remove the NOTICE.

    
answered by 16.03.2018 / 00:53
source
0

Another solution may be that you deactivate only the E_NOTICE type error notifications with this line at the beginning of your code.

<?php

error_reporting(E_ALL ^ E_NOTICE);
    
answered by 16.03.2018 в 01:13