How to create foreach (PHP/SQL)

Asked

Viewed 132 times

4

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

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

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

In SQL, I’m doing like 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. Duplicated fields are ignored.

They can give me a light there?

Thank you in advance ;)

  • 3

    I don’t know why the foreachs are aligned ... when you put one ; in foreach he dies right there, in these situations use a delimited block { and } and do not use obsolete functions.

  • didn’t work :/

  • The keys were just a hint, you need to explain why the foreach are aligned, that $i also doesn’t make much sense. You need to explain what the purpose of this code is.

1 answer

1

Well the problem is that you are doing wrong your "for’s" either with the ";" killing it as the comemntario or wanting to run 2x, the correct is only to go through only one matrix is the "value" or is the "service" and from the key of it do the insertion of the data; Another mistake is that you are mixing the data of the formuatio because one part for what gives to percerber is matrix and the other part is not.

But let’s go to a more practical example ... first the HTML that I believe has 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 the PHP itself ... I’m only point the part that matters that is the foreach the rest I think you already know what you do...

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')");  
}   
?>

Browser other questions tagged

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