I have a system that brings the database of users as follows:
Forthis,thecodelookslikethis:
publicfunctionlistarUsuarios($id,$usuarios){.....$listar.="<div style='display=block'><table class='table table-bordered'>
<thead>
<tr>
<th style='background-color: #4682B4; color: #FFF; text-align: center'>USUÁRIO</th>
<th style='background-color: #4682B4; color: #FFF; text-align: center'>COMPARECEU</th>
<th style='background-color: #4682B4; color: #FFF; text-align: center'>JUSTIFICATIVA</th>
</thead>
<tbody>";
while($jmListar = mysqli_fetch_object($sqlListar)){
$listar .= "<tr>";
$listar .= "<td>
<input type='hidden' name='Usuarios[]' value='".$jmListar->IdCadastros."'>
<i class=\"fa fa-caret-right\" aria-hidden=\"true\"></i> ".$jmListar->NomeUsuarios."
</td>
<td>
<div class='radio-group'>
<label class='radio-label'>
<input name='Presenca[".$jmListar->IdCadastros."]' type='radio' checked='checked' value='S' onclick=\"desabilitar('N')\">
<span class='inner-label' style='color: #008000; font-weight: bold'>Sim</span>
</label>
<label class='radio-label'>
<input name='Presenca[".$jmListar->IdCadastros."]' type='radio' value='N' onclick=\"desabilitar('S')\">
<span class='inner-label' style='color: #F00; font-weight: bold'>Não</span>
</label>
</div>
</td>";
$listar .= "<td><textarea name='Justificado[]' class='form-control' id='justificativa' disabled></textarea></td>";
$listar .= "</tr>";
}
$listar .= "</table></form>";
return $listar;
}
But by registering, it is returning me this way:
INSERT INTO pe_presenca VALUES(null,'1','N','teste Justificativa', NOW());
INSERT INTO pe_presenca VALUES(null,'57','S','', NOW());
In the second column the user ID is stored, with ID 1 being Fernando Pessoa and ID 57 is Carlos Chagas.
What is happening is that he is registering the wrong way, since Carlos Chagas (ID 57) received No and justification , not the Fernando Pessoa (ID 1). The code looks like this:
if($_POST){
$usuarios = $_POST["Usuarios"];
$presenca = $_POST["Presenca"];
$justificativa = $_POST["Justificativa"];
echo $metodos->cadastrarPresenca($usuarios,$presenca,$justificar);
}
And the method I am registering:
public function cadastrarPresenca($usuarios,$presenca,$justificar){
....
list($dia,$mes,$ano) = explode("/",$dataPresenca);
$dataPresenca = $ano."-".$mes."-".$dia;
for($i = 0; $i <= count($usuarios); $i++){
$presenca[$i] = ($presenca[$i] == "S")?'S':'N';
echo "INSERT INTO pe_presenca VALUES(null,'".$usuarios[$i]."','".$presenca[$i]."','".$justificar[$i]."', NOW());";
}
exit;
.....
}
I would like the registration to be done in the correct order. Can anyone tell me where the error is? I believe it's in the way I'm getting the value of Presenca[".$jmListar->IdCadastros."]
presence, but I see no other way to get the value without it being that way.