Foreach and Mysql

2

Dear colleagues.

I am bringing two questions from the Mysql bank, from which the user will register their questions. See:

....
<input type="hidden" name="Pergunta[]" value="<?php echo $idPergunta; ?>">
Pergunta: <?php echo $jmPerguntas->Perguntas; ?><br>
Resposta: <input type="text" name="Respostas[]" class="formRespostas">
....

To retrieve the value of the answer I'm using the foreach, but how do I know which answer refers to which question?

foreach($respostas as $resposta){
      $sqlCadastrar = mysqli_query($conexao, "INSERT INTO respostas VALUES(null,".$pergunta.",".$usuario.",'".$resposta."',NOW())");         
    }
    
asked by anonymous 30.06.2015 / 17:56

1 answer

1

I managed to resolve. Follow below:

for($i = 0; $i < count($_POST['Pergunta']); $i++){
     $sqlCadastrar = mysqli_query($conexao, "INSERT INTO respostas VALUES(null,".$_POST['Pergunta'][$i].",".$usuario.",'".$_POST['Respostas'][$i]."',NOW())");     
}

 ....
<input type="hidden" name="Pergunta[]" value="<?php echo $idPergunta; ?>">
Pergunta: <?php echo $jmPerguntas->Perguntas; ?><br>
Resposta: <input type="text" name="Respostas[]" class="formRespostas">
....

Thank you all!

    
30.06.2015 / 18:55