Array only brings the first result

0

I have a form where the answers will be registered and if there is also a field to mark the true one:

...
<tr>
    <td style="font-weight: bold">1.</td>
    <td><input type="text" name="Respostas[]" class="form-control"></td>
    <td style="text-align: center"><input type="radio" name="Verdadeira[]" value="S"></td>
</tr>
<tr>
    <td style="font-weight: bold">2.</td>
    <td><input type="text" name="Respostas[]" class="form-control"></td>
    <td style="text-align: center"><input type="radio" name="Verdadeira[]" value="S"></td>
</tr>
<tr>
    <td style="font-weight: bold">3.</td>
    <td><input type="text" name="Respostas[]" class="form-control"></td>
    <td style="text-align: center"><input type="radio" name="Verdadeira[]" value="S"></td>
</tr>
...

I'm recovering as follows to register with the bank:

if ($_POST) {   
    for($i = 0; $i < count($_POST["Respostas"]); $i++){
        mysqli_query($conexao,"INSERT INTO pe_perguntas VALUES(null,'" . $_SESSION["IdPerguntas"] . "','" . $_POST["Respostas"][$i] . "','" . $_POST["Verdadeira"][$i]."');";     
}

But if I mark the third question as true, it always returns the first answer to me as the true one, not the third. See below the print ():

INSERT INTO pe_perguntas VALUES(null,'2','Resposta 1','S');
INSERT INTO pe_perguntas VALUES(null,'2','Resposta 2','');
INSERT INTO pe_perguntas VALUES(null,'2','Resposta 3','');
    
asked by anonymous 03.02.2018 / 02:10

1 answer

1

I decided the following way, I do not know if it is the right one:

...
<tr>
    <td style="font-weight: bold">1.</td>
    <td><input type="text" name="Respostas[]" class="form-control"></td>
    <td style="text-align: center"><input type="radio" name="Verdadeira_0" value="S"></td>
</tr>
<tr>
    <td style="font-weight: bold">2.</td>
    <td><input type="text" name="Respostas[]" class="form-control"></td>
    <td style="text-align: center"><input type="radio" name="Verdadeira_1" value="S"></td>
</tr>
<tr>
    <td style="font-weight: bold">3.</td>
    <td><input type="text" name="Respostas[]" class="form-control"></td>
    <td style="text-align: center"><input type="radio" name="Verdadeira_2" value="S"></td>
</tr>
...

And in PHP:

$verdadeira[$i] = ($_POST["Verdadeira_".$i.""] == "S")?"S":"N";

mysqli_query($conexao,"INSERT INTO pe_perguntas VALUES(null,'" . $_SESSION["IdPergunta"] . "','" . $_POST["Respostas"][$i] . "','" .$verdadeira[$i]."');");
    
03.02.2018 / 02:59