0
I created a method to upload clients via csv files. It is working perfectly the registration, however I need to ignore the first line, the header of the file, I am looking for but still not getting a solution. If anyone can suggest an idea.
public function uploadDados(array $dados) {
$this->dados = $dados;
if(!empty($this->dados)){
if(is_uploaded_file($_FILES['file']['tmp_name'])){
$handle = fopen($_FILES['file']['tmp_name'], "r");
while(($data = fgetcsv($handle, 1000, ";")) !== FALSE ){
if($this->verificaDados($data[0])) {
$query = $this->conn->prepare(" INSERT INTO tb_clients (id, codigo_cliente, nome_cliente, tentativas, data_venda, prioridade, data_limite, produto, status_id, timed_expirado) VALUES (null, :codigo_cliente, :nome_cliente, :tentativas, :data_venda, :prioridade, :data_limite, :produto, :status_id, :timed_expirado)");
$limite = $this->countDays($data[3]);
$query->bindParam(':codigo_cliente', $data[0]);
$query->bindParam(':nome_cliente', $data[1]);
$query->bindParam(':tentativas', $data[2]);
$query->bindParam(':data_venda', $data[3]);
$query->bindParam(':prioridade', $data[4]);
$query->bindParam(':data_limite', $limite);
$query->bindParam(':produto', $data[6]);
$query->bindParam(':status_id', $data[7]);
$query->bindParam(':timed_expirado', $data[8]);
$query->execute();
$this->resultado = TRUE;
}
}
fclose($handle);
}
}
}
In which line of your code you start going through the file line by line?
– Woss
$this->verified($date[0]) // Checks if the client does not exist in the database and returns true
– Valdir_Silva
And what the
verificaDados
does currently if, from what he commented, is not checking the data?– Woss
checked Data makes a select in the database before entering a new client if the number of affected rows is greater than or equal to 1 then returns FALSE. Returning false does not insert the record that already exists in the database.
– Valdir_Silva
I already say that this is a bad practice, affecting not only the performance but generating a race condition problem. Instead, seek to add the column as a single index in the database and handle errors during insertion.
– Woss