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.