Insert Multiple record in PHP MYSQL table

Asked

Viewed 2,245 times

5

I’m trying to make a INSERT using PHP to insert multiple BD records at once.

My code is just entering the first line of data. It even presents all lines, but, to insert, only the first is sent to the BD.

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>

File example:

00030000020001000000021808140730
00030000020001000000021808141100
00030000020001000000021808141300
00030000020001000000021808141750
00030000030001000001231708140900
00030000030001000001231708141200
00030000030001000001231708141300
00030000030001000001231708141800
  • 4

    Make sure you are not trying to insert duplicate data into some single-key column.

  • 6

    Value also exists! Values is usually used when you intend to add more than 1 record in the same query type: "values (...), (...), (...)". Jerry he presents some mistake?

  • 1

    I know it doesn’t have much to do, but I didn’t understand why the <form> is in the loop and closing only outside the loop?!

  • 3

    Change mysql_query($sql_gravar); for mysql_query($sql_gravar) or die(mysql_error()); see if an error is displayed.

  • 1

    Follow the @Premiere tip, inside the loop mount the QUERY with the values and run 1 time then

  • Hello! people. I put it to give the error.. and actually it generated Key Duplication. ie, all vc s were right. I will put to update and check if you have records. Thank you very much!

Show 1 more comment

2 answers

1

I did some tests whose at this link http://axitech.com.br/teste.php I managed to have all the records returned within the while reason why I think now you will be able to do the INSERT without problems from the instruction of while.

I’ll put the code here whose data storage file is this: http://axitech.com.br/coleta.txt ... Basically the errors were in the instructions PHP, actually was giving PHP error that I detected while pasting the code into Dreamweaver.

Here is your code minimally modified (I commented some parts for example related to connection to the database). Ahhhh, I put the table before the form and the two outside the 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 discolour the entry part in the database and also reactivate errors and warnings to show again if your php has errors, just comment to avoid appearing errors related to php mysql.

  • 2

    you can use the http://www.phpfiddle.org or other similar

  • Hello! Marcos I did a test with your Code. it did not fail. But, I had a problem q needed to solve as the conversation I had above with the other partners.. I had a problem with the Primary Key. I’ll try to solve it. Anyway thank you very much for your attention. helped me with the Form tb issue. Hug

0

I recommend using function explode:

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

Then you give a:

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.