0
I have the following line of code that performs the txt file import.
<?php
function Inserir($itens, Pdo $pdo){
$sts = $pdo->prepare("INSERT INTO dados(loja, cod_prod, cod_acesso, desc_prod, estoq_disp, data, estoq_validade) VALUES(?,?,?,?,?,?,?);");
$sts->bindValue(1, $itens[0], PDO::PARAM_STR);
$sts->bindValue(2, $itens[1], PDO::PARAM_STR);
$sts->bindValue(3, $itens[2], PDO::PARAM_STR);
$sts->bindValue(4, $itens[3], PDO::PARAM_STR);
$sts->bindValue(5, $itens[4], PDO::PARAM_STR);
$sts->bindValue(6, $itens[5], PDO::PARAM_STR);
$sts->bindValue(7, $itens[6], PDO::PARAM_STR);
$sts->execute();
$sts->closeCursor();
$sts = NULL;
}
if (!empty($_FILES['arquivo']))
{
$Pdo = new PDO("mysql:host=localhost;dbname=vencimento", "root", "");
$file = fopen($_FILES['arquivo']['tmp_name'], 'r');
while (!feof($file)){
$linha = fgets($file);
$itens = explode(';', $linha);
Inserir($itens, $Pdo);
}
}
?>
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Arquivo</title>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" enctype="multipart/form-data" method="post">
<input type="file" name="arquivo" id="arquivo">
<input type="submit" name="enviar" value="Enviar">
</form>
</body>
</html>
I need that at the time of entering in the database the lines of the file that are exactly the same as those already existing within the database it ignore that line and insert only those that are not equal.
Before calling the function
Inserir()
, you need to check if that current line does not already exist in the bank, if it exists, does nothing, if it does not exist, calls the functionInserir()
.– Leandro Lima
@Leandrolima I understood, I just don’t know how I would put this function inside this code, you can help me in this?
– Alex Sousa