0

I'm having a problem passing values of inputs with same name, that was working but now tha just doesnt work anymore here is the html and the php

html:

<form action="alterado.php" method="post">
<p class="codigo"><input type="text" name="codigo-peca[]"></p>
<p class="descricao-peca"><input type="text" name="descricao-peca[]"></p>
<p class="valor-peca">R$<input type="text" name="valor-peca[]"></p>

<p class="codigo"><input type="text" name="codigo-peca[]"></p>
<p class="descricao-peca"><input type="text" name="descricao-peca[]"></p>
<p class="valor-peca">R$<input type="text" name="valor-peca[]"></p>

<input type="submit" value="Salvar Dados">

alterado.php

   if(is_array($_POST['valor-peca']) && is_array($_POST['codigo-peca']) && is_array($_POST['descricao-peca'])  ) {
    // ... code
    for($i = 0; $i < count($_POST['valor-peca']); $i++) {
        // ... reference index of arrays
        $valorPeca = $_POST['valor-peca'][$i];
        $codigoPeca = $_POST['codigo-peca'][$i];
        $descricaoPeca = $_POST['descricao-peca'][$i];

        if ($valorPeca != 0){
        $SQL = "INSERT INTO pecas (ordemServico, codigoPeca, descricaoPeca, valorPeca) VALUES ('$ordem', '$codigoPeca', '$descricaoPeca', '$valorPeca');";
$result = mysql_query($SQL);
}
        }
    }

but that is reading only the first input value but not the second

Bertucci
  • 1
  • 2
  • **Danger**: You are using [an **obsolete** database API](http://stackoverflow.com/q/12859942/19068) and should use a [modern replacement](http://php.net/manual/en/mysqlinfo.api.choosing.php). You are also **vulnerable to [SQL injection attacks](http://bobby-tables.com/)** that a modern API would make it easier to [defend](http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php) yourself from. – Quentin Mar 27 '14 at 17:29
  • I don't think this is the problem, but check if there'd be any difference if you add value="" into the input tags. – Miroslav Genev Mar 27 '14 at 17:35
  • i checked that Miroslav but doesnt work either – Bertucci Mar 27 '14 at 17:40

1 Answers1

0

You are overwriting the the variables $valorPeca, $codigoPeca and $descricaoPeca. It will only show one variable at a time unless you append them together.

if(is_array($_POST['valor-peca']) && is_array($_POST['codigo-peca']) && is_array($_POST['descricao-peca'])  ) {
            $valorPeca = '';
            $codigoPeca = '';
            $descricaoPeca = ''
            for($i = 0; $i < count($_POST['valor-peca']); $i++) {
                // ... reference index of arrays
                $valorPeca .= $_POST['valor-peca'][$i] . " ";
                $codigoPeca .= $_POST['codigo-peca'][$i] . " ";
                $descricaoPeca .= $_POST['descricao-peca'][$i] . " ";
            }
}
MrHunter
  • 1,892
  • 1
  • 15
  • 23
  • sorry i didnt post everything, i am using that to input in a db, so i just need one at a time, but whenever i add the query to save that just save the first, i post the full code of php now – Bertucci Mar 27 '14 at 17:26