Inserir vários registros
Bom dia, tenho um formulário que lança vários dados ao mesmo tempo. só que não estou conseguindo! me ajudem por gentileza!
Código do Formulário
<form action="action_financeiro.php" method="post" id='form-contato' enctype='multipart/form-data'>
<?php if(!empty($clientes)):?>
<!-- Tabela de Clientes -->
<table class="table table-striped">
<tr class='active'>
<th width="13%" style="font-size:14px">N Recibo</th>
<th width="11%" style="font-size:14px">Data</th>
<th width="8%" style="font-size:14px">Registro</th>
<th width="9%" style="font-size:14px">Função</th>
<th width="20%" style="font-size:14px">Nome</th>
<th width="13%" style="font-size:14px">Dinheiro</th>
<th width="13%" style="font-size:14px">Seguro Vida</th>
<th width="13%" style="font-size:14px">Outros</th>
</tr>
<?php foreach($clientes as $cliente):?>
<tr>
<td class="image"> <input type="text" class="form-control input-sm" id="recibo" name="recibo" placeholder="123.456" autocomplete="off" onfocus="this.style.backgroundColor='#FFCC00'" onblur="this.style.backgroundColor='#E8E8E8'"></td>
<td class="image"><input type="date" class="form-control input-sm" id="data" name="data" autocomplete="off" onfocus="this.style.backgroundColor='#FFCC00'" onblur="this.style.backgroundColor='#E8E8E8'"></td>
<td><span style='font-weight:bold; font-size:12px'><?=$cliente->id?></span></td>
<td style='font-weight:bold; font-size:11px'><span style='font-weight:bold;'><?=$cliente->funcao?></span></td>
<td><span style='font-weight:bold; font-size:12px'><?=$cliente->nome?></span></td>
<td><input type="text" class="form-control input-sm" id="dinheiro" name="dinheiro" onkeydown="FormataMoeda(this,10,event)" onkeypress="return maskKeyPress(event)" onfocus="this.style.backgroundColor='#FFCC00'" onblur="this.style.backgroundColor='#E8E8E8'"></td>
<td><input type="text" class="form-control input-sm" id="seguro" name="seguro" onkeydown="FormataMoeda(this,10,event)" onkeypress="return maskKeyPress(event)" onfocus="this.style.backgroundColor='#FFCC00'" onblur="this.style.backgroundColor='#E8E8E8'"></td>
<td><input type="text" class="form-control input-sm" id="outros" name="outros" onkeydown="FormataMoeda(this,10,event)" onkeypress="return maskKeyPress(event)" onfocus="this.style.backgroundColor='#FFCC00'" onblur="this.style.backgroundColor='#E8E8E8'">
</td>
</tr>
<?php endforeach;?>
</table>
<?php else: ?>
<!-- Mensagem caso não exista clientes ou não encontrado -->
<h3 class="text-center text-primary">Faça uma busca!</h3>
<?php endif; ?>
</fieldset>
</fieldset>
<fieldset>
<!--<input type="hidden" class="form-control" id="registro" name="registro" value="<?=$cliente->id?>">
<input type="hidden" class="form-control" id="funcao" name="funcao" value="<?=$cliente->funcao?>">
<input type="hidden" class="form-control" id="nome" name="nome" value="<?=$cliente->nome?>"> -->
<input type="hidden" name="acao" value="incluir">
<button type="submit" class="btn btn-primary" id='botao'>
Gravar
</button>
<a href='ministros.php' class="btn btn-danger">Cancelar</a>
</form>
O arquivo Action responsável por salvar os dados!
<?php
require 'Connections/conexao.php';
// Atribui uma conexão PDO
$conexao = conexao::getInstance();
// Recebe os dados enviados pela submissão
$acao = (isset($_POST['acao'])) ? $_POST['acao'] : '';
$id = (isset($_POST['id'])) ? $_POST['id'] : '';
$recibo = (isset($_POST['recibo'])) ? $_POST['recibo'] : '';
$data = (isset($_POST['data'])) ? $_POST['data'] : '';
$registro = (isset($_POST['registro'])) ? $_POST['registro'] : '';
$funcao = (isset($_POST['funcao'])) ? $_POST['funcao'] : '';
$nome = (isset($_POST['nome'])) ? $_POST['nome'] : '';
$dinheiro = (isset($_POST['dinheiro'])) ? $_POST['dinheiro'] : '';
$seguro = (isset($_POST['seguro'])) ? $_POST['seguro'] : '';
$outros = (isset($_POST['outros'])) ? $_POST['outros'] : '';
// Valida os dados recebidos
$mensagem = '';
if ($acao == 'editar' && $id == ''):
$mensagem .= '<li>ID do registros desconhecido.</li>';
endif;
// Se for ação diferente de excluir valida os dados obrigatórios
if ($acao != 'excluir'):
if ($mensagem != ''):
$mensagem = '<ul>' . $mensagem . '</ul>';
echo "<div class='alert alert-danger' role='alert'>".$mensagem."</div> ";
exit;
endif;
endif;
// Verifica se foi solicitada a inclusão de dados
if ($acao == 'incluir'):
$valores = range( 1 , 10 );
$sql = sprintf( 'INSERT INTO financeiro (recibo, data, registro, funcao, nome, dinheiro, seguro, outros)
VALUES(:recibo, :data, :registro, :funcao, :nome, :dinheiro, :seguro, :outros)', implode( '), (' , $valores )) ;
$stm = $conexao->prepare($sql);
$stm->bindValue(':recibo', $recibo);
$stm->bindValue(':data', $data);
$stm->bindValue(':registro', $registro);
$stm->bindValue(':funcao', $funcao);
$stm->bindValue(':nome', $nome);
$stm->bindValue(':dinheiro', $dinheiro);
$stm->bindValue(':seguro', $seguro);
$stm->bindValue(':outros', $outros);
$retorno = $stm->execute();
if ($retorno):
echo "<div class='alert alert-success' role='alert'>Registro inserido com sucesso, aguarde você está sendo redirecionado ...</div> ";
else:
echo "<div class='alert alert-danger' role='alert'>Erro ao inserir registro!</div> ";
endif;
echo "<meta http-equiv=refresh content='0;URL=financeiro.php'>";
endif;
Só insere a primeiro linha do formulário.
Tentei adaptar esta função: [https://pt.stackoverflow.com/questions/54410/como-gravar-varios-registros-em-uma-tabela-ao-mesmo-tempo-mysql](https://pt.stackoverflow.com/questions/54410/como-gravar-varios-registros-em-uma-tabela-ao-mesmo-tempo-mysql)Discussão (15)
Carregando comentários...