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
Make sure you are not trying to insert duplicate data into some single-key column.
– Bacco
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?
– Premiere
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?!
– Marcelo Diniz
Change
mysql_query($sql_gravar);
formysql_query($sql_gravar) or die(mysql_error());
see if an error is displayed.– rray
Follow the @Premiere tip, inside the loop mount the QUERY with the values and run 1 time then
– Papa Charlie
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!
– Jerry Silva