How to create foreach (PHP / SQL)

4

I'm trying to send from a form to html, a table, with two fields with Array[] . These two fields are Service and Value, I want to add more than one product in form , and it inserts into different records in my table.

I did this: I duplicate the fields in javascript, and send them all together by POST.

  <input name="valor[]" type="text" id="valor[]" size="6">

In SQL, I'm doing this:

$carro=strtoupper($_POST['carro']);
$placa=strtoupper($_POST['placa']);
$cor=strtoupper($_POST['cor']);
$hora=strtoupper($_POST['hora']);
$proprietario=$_POST['proprietario'];

$i = 0;

 foreach($_POST["servico"] as $id => $servico);
 foreach($_POST["valor"] as $id => $valor);

$telefone=strtoupper($_POST['telefone']);
$buscar=strtoupper($_POST['buscar']);
$obs=strtoupper($_POST['obs']);
$data=strtoupper($_POST['data']);
$ativo=strtoupper($_POST['ativo']);

   {
mysql_query("INSERT INTO agenda (carro, placa, cor, hora, proprietario, servico, valor, telefone, buscar, obs, data, ativo) VALUES ('$carro', '$placa', '$cor', '$hora', '$proprietario', '{$_POST['servico'][$i]}', '{$_POST['valor'][$i]}', '$telefone', '$buscar', '$obs', '$data', 'SIM')");

    ++$i;
        };


$i = 0;

However, it saves only the first line. The duplicate fields are ignored.

Can you give me a light there?

Thank you in advance;)

    
asked by anonymous 26.04.2016 / 21:51

1 answer

1

Well the problem is that you are doing your "for's" wrong with the ";" it's just the "value", that is, the "service" and from the key of it to do the insertion of the data; Another error is that you are mixing the data of the formuatio because one part for what to know is matrix and the other part is not.

But let's take a more practical example ... first the HTML that I believe you have no doubts ...

HTML

<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post">
<!-- Outros campos do seu Formulátrio -->

Serviço/Valor #1:<br>
<input name="servico[]" type="text" id="servico[]" size="10">
<input name="valor[]" type="text" id="valor[]" size="6"><br>

Serviço/Valor #2:<br>
<input name="servico[]" type="text" id="servico[]" size="10">
<input name="valor[]" type="text" id="valor[]" size="6"><br>

Serviço/Valor #3:<br>
<input name="servico[]" type="text" id="servico[]" size="10">
<input name="valor[]" type="text" id="valor[]" size="6"><br><br> 

<input type="submit">
</form>

And the part that matters PHP in itself ... I'm only point to the part that matters what is foreach the rest I think of already know what it does ...

PHP

<?php
/* Campos do formulario */
$carro = strtoupper($_POST["carro"]);
$placa = strtoupper($_POST["placa"]);
$cor   = strtoupper($_POST["cor"]);
/* Seus outros campos entra aqui */

// Faz a 'leitura' de todos os campos serviço
// Trazendo sua posição (key) e valor 
foreach ($_POST["servico"] as $chave => $valor) {
 $dados_servico = $valor; // Adiciona o valor a variável apenas visual
 $dados_valor = $_POST["valor"][$chave]; // Com a chave pega a mesma posição porem do campo "valor" 

 // Insere no Banco
 mysql_query("INSERT INTO agenda (carro, placa, cor, hora, proprietario, servico, valor, telefone, buscar, obs, data, ativo) VALUES ('$carro', '$placa', '$cor', '$hora', '$proprietario', '$dados_servico', '$dados_valor', '$telefone', '$buscar', '$obs', '$data', 'SIM')");  
}   
?>
    
26.04.2016 / 22:41