I'm importing some data into the table "tb_usuario" that comes from a text file, it works as a layout for importing these records, I used a semicolon to delimit the position of this data in the file, causing it to be inserted in the database of data, numero
and situacao
of each user.
I would like to add a status
field also in the DB, but the question is when it is time to insert the insert because for each type of situation a different value is assigned:
status = 1 for active and status = 2 for inactive.
I would have to compare the position situacao
of each line before inserting in the DB, if it is the situation is "ACTIVE" assign status = 1 if it is "INACTIVE" assign status = 2
Ex:
file.txt (Layout)
NUMERO | SITUAÇÃO
1 | ATIVO
2 | INATIVO
After insert
should look like this in DB:
tb_user
ID | NUMERO | SITUACAO | STATUS
1 1 ATIVO 1
2 2 INATIVO 2
importer.php
function Inserir($itens, Pdo $pdo){
$sts = $pdo->prepare("
INSERT INTO tb_usuario(numero,situacao)
VALUES(?,?);
");
$sts->bindValue(1, $itens[0], PDO::PARAM_STR);
$sts->bindValue(2, $itens[1], PDO::PARAM_STR);
$sts->execute();
$sts->closeCursor();
$sts = NULL;
}
if (!empty($_FILES['arquivo']))
{
$Pdo = new PDO("mysql:host=localhost;dbname=teste", "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>Importar 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>