Picking the wrong id when selecting line with CheckBox

0

In this script I loop the rows of a table, where each row contains a checkbox and hidden inputs.

The problem is that when I select a line with the checkbox, the values corresponding to the checkbox come correct plus the other inputs come as taking data from the unmarked lines

test.php

<formid="form2" name="form2" action="acao.php form=cotacao" method="POST">
<table>    
<tr>
<td><input name="checkbox[]" type="checkbox" value="<?echo $id_produtos>"/>  
</td>
<td><?echo $id_produtos?></td>
<td><input type="hidden" name="nome[]"  value="<?echo $nome?>" /></td>
<td><input type="hidden" name="tipo_serv[]"  value="<?echo $tipo_serv?>" />  
</td>
<td><input type="hidden" name="valor[]"value="<?echo $valor?>" />  
</td> 
</tr><input type="submit" value="Gerar"/>   

     acao.php

    if (isset($_POST['checkbox'])) {
    foreach ($_POST['checkbox'] as $key => $value) {
    echo $id_saida = mysql_real_escape_string($value);
         $tipo = mysql_real_escape_string($_POST['tipo_serv'][$key]);
    echo $nome = mysql_real_escape_string($_POST['nome'][$key]);
         $start = mysql_real_escape_string($_POST['start'][$key]);
    echo $valor = mysql_real_escape_string($_POST['valor'][$key]);
         $desconto = mysql_real_escape_string($_POST['desconto'][$key]);
        $tipo_veiculo=mysql_real_escape_string($_POST['tipo_veiculo'[$key]);
    
asked by anonymous 07.10.2015 / 22:18

2 answers

1

In order to make the relation using the id field, the name attribute, whose value is checkbox[] , must have the value of id indexed so that you can read and sign the correct values with foreach .

In your script you should simply print a echo as an index for your checkbox[] array, this index should be the same for the name attribute of the product. In your case, it would look something like this:

checkbox[<?php echo $id_produtos; ?>]

Below is another example of how it would look, based on a separate script:

<?php

// Ler dados enviados pelo formulario gerado
if(isset($_POST["enviar"])){
    foreach($_POST["checkbox"] as $key=>$value){        
        echo "ID: " . $id . " Nome: " . $_POST["nome"][$key] . "<br/>";
    }   
}


// Conexao
// Eu usei uma tabela local para elaborar este exemplo
$mysqli = new mysqli("localhost", "root", "", "example");
$sql = "SELECT * FROM jogos";
$form = null;

if($query = mysqli_query($mysqli, $sql)){
    while($linha = mysqli_fetch_object($query)){
        $form .= "<input type=\"checkbox\" name=\"checkbox[{$linha->id}]\" value=\"{$linha->id}\"/>{$linha->nome}<br/>";    
        $form .= "<input type=\"hidden\" name=\"nome[{$linha->id}]\" value=\"{$linha->nome}\"/>";
    }   

    // Montar o resto do formulário

    ?>
    <hr>
    <form method="POST" action="">
    <!-- Resto !-->
    <?php echo $form; ?>
    <!-- Resto !-->
    <input type="submit" name="enviar" value="enviar"/>
    </form>

    <?php
}

?>  

Another thing is that I did not understand exactly how this script is structured, since in one part this is related to automobiles and in another I just do not know what it is. This was one of the reasons why I had to come up with an example of my own.

Another recommendation is for MySQL functions that are deprecated , use MySQL or PDO instead .

References:

PDO - PHP.net

MySQLi - PHP.net

    
09.10.2015 / 03:15
0

If I get it right, you want to send multiple lines, right? The way you are doing is just sending a form, as you are putting a form for each line, I think the following can help:

<form id="form2" name="form2" action="acao.php form=cotacao" method="POST">
<table>
<?php
$cmd = "SELECT * FROM produtos where id_transfer = '$id_transfer' AND   
tipo_serv <> 'Regular'  ";

    $produtos = mysql_query($cmd);
    $total = mysql_num_rows($produtos);
    while ($linha = mysql_fetch_array($produtos)) {

 $id_produtos = $linha['id_produtos'];
 $nome = $linha['nome'];
 $valor = $linha['valor'];
 $tipo_serv = $linha['tipo_serv'];
 ?>
<tr>
<td><input name="checkbox[]" type="checkbox" value="<?echo $id_produtos>"/>  
</td>
<td><?echo $id_produtos?></td>
<td><input type="hidden" name="nome[]"  value="<?echo $nome?>" /></td>
<td><input type="hidden" name="tipo_serv[]"  value="<?echo $tipo_serv?>" />  
</td>
<td><input type="hidden" name="valor[]"value="<?echo $valor?>" />  
</td> 
</tr><input type="submit" value="Gerar"/>   
<?php}?>
</table>
</form>
    
07.10.2015 / 22:31