Error importing xls into postgres

1

I'm trying to import an Xls file into the PostGresSql database, however when I run the submit on the page itself, the error below occurs:

Undefined offset: 1 in C: \ wamp \ www \ projects ... line 59

Below is the code:

<div class="container-fluid"> 
	<fieldset class="bottom">
		<legend>Importação em Excel</legend>	
			<form action="#" method='post' enctype="multipart/form-data">
		<fieldset>
			<div class="form-group">
				<input type='file' name='sel_file' size='20'>
			</div>
		</fieldset>
	<input type='submit' name='submit' value='Importar' class="btn btn-success">
</form>

<?php
	
if(isset($_POST["submit"])){	

	if(!@($conexao=pg_connect("host=localhost port=5432 dbname=teste user=postgres password=****"))) {
		print "Não foi possível estabelecer uma conexão com o banco de dados.";
	} else {
	   print "Conexão OK!"; 
	}

     $fname = $_FILES['sel_file']['name'];
     $chk_ext = explode(".",$fname);

     if(strtolower($chk_ext[1]) == "xls"){
    
         $filename = $_FILES['sel_file']['tmp_name'];
         $handle = fopen($filename, "r");
		 
         while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
			 
           $sql = "INSERT into items(nome,email) values('$data[0]','$data[1]')";
 		   $result = pg_query($conexao, $sql);
         }
   
         fclose($handle);
         echo "Arquivo Importado com sucesso";
     }else{
         echo "Arquivo inválido";
     } 
 
}

?>
	

The table structure is very simple:

AndtheXlsFile:

Thank you in advance for your attention.

    
asked by anonymous 13.12.2017 / 17:11

2 answers

0

Solved!

if ($getLinha == ''){
    $getLinhaValor = 6;
}else {
    $getLinhaValor = $getLinha ;
}

//obten a linha do arquivo conforme a escolha do usuario
if ($linha_count > $getLinhaValor ){
   ..... code
}
    
19.12.2017 / 12:31
1

I was able to insert the code below:

 $file   = $_FILES ['sel_file' ]['tmp_name'] ; 
	  $row = 1;
		if (($handle = fopen($file, "r")) !== FALSE) {
			while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
				//$num = count($data);
				$row++;

				$veiculo = $data[0]; 
				$rd =$data[1];

				$sql = "INSERT into items(veiculo,rd) values('".$veiculo."','".$rd."')";
				$rs = pg_query($conexao, $sql);
			}
			
			if ($rs){ 
				echo   "Arquivo importado com sucesso !" ; 
				fclose($handle);
			}else{ 
				echo   "Erro ao carregar o arquivo" ; 
			} 
		}

So I would like to get only the lines starting at line 6, ignoring the header as below:

    
13.12.2017 / 20:15