1
I am trying to import an XLS file into the Postgressql database, however when I run Submit on the page itself, the error below occurs:
Undefined offset: 1 in C: wamp www projects... line 59
Below, follow 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 :
And the XLS File :
Thanks in advance for your attention.
Variables
Array
must be concatenated in SQL.: Ex.:('".$data[0]."','".$data[1]."')
– William Aparecido Brandino
even then the error occurs: Undefined offset: 1 in C: wamp www urm projects... line 59 Line 59 -> $sql = "INSERT into items(name,email) values('". $data[0]." ','". $date[1]."')";
– hulckb
And the data, it comes correctly?
– William Aparecido Brandino