Insert Multiple Record in PHP MYSQL table

5

I'm trying to make a INSERT using PHP to insert multiple records into the DB at once.

My code is only inserting the first row of the data. It even displays all the lines, but to insert, only the first one is sent to the DB.

include "conexao.php";

$arquivo = "txt/Coleta.TXT";
$objeto =  fopen($arquivo, 'r');

while($dados = fgets($objeto))
{
    $empresa = trim(substr($dados, 0, 4));
    $funcionario = trim(substr($dados, 4, 6));
    $local = trim(substr($dados, 10, 4));
    $cracha = trim(substr($dados, 14, 8));
    $data = trim(substr($dados, 22, 6));

    $data_dia = substr($data, 0, 2);    
    $data_mes = substr($data, 2, 2);
    $data_ano = substr($data, 4, 2);    
    $datames = "20$data_ano"."$data_mes"."$data_dia"; 
    $hora = trim(substr($dados, 28, 4));


    $sql_gravar = ("INSERT INTO cadhorames(codempre, codfunci, localfun, crachfun, datamfun, horafun) VALUE 
                  ('$empresa','$funcionario','$local','$cracha','$datames','$hora')");

    mysql_query($sql_gravar);


    ?>

<form name="arqtxt" >
    <table>
        <tr>
            <td><? echo $empresa ?> </td>
            <td><? echo $funcionario ?> </td>
            <td><? echo $local ?> </td>
            <td><? echo $cracha ?> </td>
            <td><? echo $datames ?> </td>
            <td><? echo $hora ?> </td>
        </tr>
    </table>
<?
}
fclose($objeto);


</form>

Sample file:

00030000020001000000021808140730
00030000020001000000021808141100
00030000020001000000021808141300
00030000020001000000021808141750
00030000030001000001231708140900
00030000030001000001231708141200
00030000030001000001231708141300
00030000030001000001231708141800
    
asked by anonymous 21.08.2014 / 20:42

2 answers

1

I did some tests whose link link managed to get all the records returned within the reason why I think that you will now be able to do INSERT with no problems from the while statement.

I'll put the code here whose file that stores the data is this: link ... Basically the errors were in the PHP instructions, I was actually giving PHP error that I detected by pasting the code in Dreamweaver.

Here is your minimally modified code (I commented some parts for example related to the connection to the database). Ahhhh, put table before form and both out while :

<?php

error_reporting(0);
ini_set("display_errors", 0);
include ("conexao.php");
$arquivo = "coleta.txt";
$objeto =  fopen($arquivo, 'r');

echo '<form action="" method="post" enctype="application/x-www-form-urlencoded">';
echo '<table>';

while($dados = fgets($objeto))
{
    $empresa        = trim(substr($dados, 0, 4));
    $funcionario    = trim(substr($dados, 4, 6));
    $local          = trim(substr($dados, 10, 4));
    $cracha         = trim(substr($dados, 14, 8));
    $data           = trim(substr($dados, 22, 6));

    $data_dia       = substr($data, 0, 2);    
    $data_mes       = substr($data, 2, 2);
    $data_ano       = substr($data, 4, 2);    
    $datames        = "20$data_ano"."$data_mes"."$data_dia"; 
    $hora           = trim(substr($dados, 28, 4));


    /*
    $sql_gravar = ("INSERT INTO cadhorames(codempre, codfunci, localfun, crachfun, datamfun, horafun) VALUE 
                  ('$empresa','$funcionario','$local','$cracha','$datames','$hora')");

    mysql_query($sql_gravar);
    */


?>

<tr>
    <td><?php echo $empresa ?> </td>
    <td><?php echo $funcionario ?> </td>
    <td><?php echo $local ?> </td>
    <td><?php echo $cracha ?> </td>
    <td><?php echo $datames ?> </td>
    <td><?php echo $hora ?> </td>
</tr>

<?php } 

echo '</table>';
echo '</form>';

fclose($objeto); 

?>

Remembering, you must

21.08.2014 / 22:21
0

I recommend using explode function:

$linhas = explode('\n',$arquivoTXT);

Then you give one:

foreach($linhas as $value) {
 // aqui você processa os dados de cada linha
 // e insere no banco de dados.
} 
    
26.08.2014 / 01:57