To send multiple fields with the same name, simply treat them as an array. To do this, place brackets in front of the name.
See
while ($linha = mysqli_fetch_array($notas, MYSQLI_ASSOC))
{
$cpfAluno = $linha["Aluno_CPF"];
$pegaNomeAluno = "SELECT Id, Nome FROM aluno WHERE CPF = '$cpfAluno' ORDER BY Nome";
$nomeAluno = mysqli_query($con, $pegaNomeAluno);
$alunoArray = mysqli_fetch_array($nomeAluno, MYSQLI_ASSOC);
$aluno = implode(', ', (array)$alunoArray);
echo '<tr>';
echo '<td>';
echo $aluno;
echo '<input type="hidden" id="aluno_id_' . $aluno['id'] . '" value="' . $aluno['id'] . '" name="aluno_id[]" class="form-control">';
echo '</td>';
echo '<td>';
echo '<input type="text" id="prova1_' . $aluno['id'] . '" maxlength="2" size="1" name="prova1[' . $aluno['id'] . ']" class="form-control">';
echo '</td>';
echo '<td>';
echo '<input type="text" id="prova2_' . $aluno['id'] . '" maxlength="2" size="1" name="prova2[' . $aluno['id'] . ']" class="form-control">';
echo '</td>';
echo '<td>';
echo '<input type="text" id="media_' . $aluno['id'] . '" maxlength="2" size="1" name="media[' . $aluno['id'] . ']" class="form-control">';
echo '</td>';
echo '</tr>';
}
Note that I put brackets filled in with the student's id ( name="media[' . $aluno['id'] . ']"
) in the name and put them in the HTML tag ID to avoid duplicate IDs and break the HTML rule, which in case this happens, leaves the code invalid
I've also added field types, text
and hidden
. Do not specify them other than invalidate, leave the code less readable.
The hidden
field I added to make it easier to wrap data when inserting into the database. And to retrieve and process the received data, do:
$alunoId = filter_input(INPUT_POST, 'aluno_id', FILTER_VALIDATE_INT);
$prova1 = filter_input(INPUT_POST, 'prova1', FILTER_VALIDATE_INT);
$prova2 = filter_input(INPUT_POST, 'prova2', FILTER_VALIDATE_INT);
$media = filter_input(INPUT_POST, 'media', FILTER_VALIDATE_INT);
foreach ( $alunoId as $id ) {
$prova1[$id]; // Recupera o valor de "prova1" do aluno com o id na váriável $id
$prova2[$id]; // Recupera o valor de "prova2" do aluno com o id na váriável $id
$media[$id]; // Recupera o valor de "media" do aluno com o id na váriável $id
// Faça a lógica do banco de dados aqui.
}
To retrieve the variables received via post, I used filter_input
to ensure a bit more security for the application, but if it does not matter to you, you can use the global variable $_POST
with no problems, just keep in mind that is not the most correct way to do it.