0
I have a table of students where they have a field of presence where they are recorded present and absent.
When I created the camp, I left everyone as absent, and now I need to list and change some to present. I’d like to change them all at once, but I don’t know how to make one update
in all records at once.
See the code of select
of the fields, where 2 means absent and 1 would be present, (I’m trying with checkbox
):
php form.
<?php
$conn = mysqli_connect($servidor, $usuario, $senha, $dbname);
$idEvent = $_POST['idsubev'];
?>
<?php
//
$sql = "SELECT u.nome, e.titulo, a.presente, a.id_al FROM sch_usuarios u INNER JOIN sch_acontecimentos e INNER JOIN sch_aluno_acont a WHERE e.id_acon = a.id_acon AND u.id = a.id_al AND e.id_subevent='$idEvent' ORDER BY u.nome";
$query = mysqli_query($conn, $sql);
while ($rows = mysqli_fetch_array($query)) {
echo "
<form method='post' action = 'update.php' >
<input type='hidden' name='id' value='".$rows['id_al']."'>
<h1> Alterar presença do aluno</h1>
<table align='' border='0' bordercolor='#BCBCBC' cellspacing='0'>
<tr align ='left' bordercolor='#000000' >
<td valign='middle'> </td>
<td valign='middle'> </td>
</tr>
<tr align ='left' bordercolor='#000000' ><td valign='middle' bgcolor='#E9E9E9'><p><font color=''>Nome:</font> </p></td>
<td align='left' valign='middle' bgcolor='#E9E9E9'><input type = 'text' size='50' name='nome' value ='".$rows['nome']."'></td>
</tr>
<tr><td><font color=''> Curso: </font> </td>
<td align='left'><input type='text' size='30' name='curso' value=' ".$rows['titulo']."'><font color=''> </font>
</td>
<tr align ='left'>
//QUERO ATUALIZAR ESSE CAMPOS ABAIXO CHAMADO PRESENTE
<td><font color=''>Presente=<b>".$rows['presente']."</b> </font></td>
<td align='left'>
//AQUI MARCAREI O CHECKBOX NOS ALUNOS QUE QUERO MUDAR PARA 1 (PRESENTE)
Status 1= presente, 2= ausente
<input type='checkbox' name='presente' value='1'>Marcar Presente?
</td>
</tr>
</table>
"; /*fecha a tabela apos termino de impressão das linhas*/
}
echo "<input type='submit' value='alterar'>
</form>";
?>
update php.
$id=$_POST['id'];
$presente = $_POST['presente'];
$mysqli = new mysqli('localhost', 'wwwcard_ew3', 'adm22334455', 'wwwcard_ew3');
$sql = "UPDATE sch_aluno_acont SET presente = '$presente' WHERE id_al = '$id'";
$stmt = $mysqli->prepare($sql) or die($mysqli->error);
if(!$stmt){
echo 'erro na consulta: '. $mysqli->errno .' - '. $mysqli->error;
}
$stmt->bind_param('ssi',$id, $presente);
$stmt->execute();
header("Location: index.php?altera_aluno");
Just take the Where, no?
– Francisco
What do you mean?? I don’t understand
– Claudio
You’re ticking 1 by 1 and then gives a Ubmit to update the ones that were ticked?
– Sam
First of all, make a backup of the table in case something goes wrong :)
– Sam
Exactly ticking the records I want to update one by one after I give the Submit...
– Claudio
by context, the fact that the student is present should be an Insert and not an update, but ok. Take the values that are marked, and give the update like this:
"UPDATE sch_aluno_acont SET presente = '$presente' WHERE id_al in (1,2,3,4,9,7,10)"
whereas(1,2,3,4,9,7,10)
are the ids of students who have been tagged.– Rovann Linhalis
But how can I get these ids because it will give more than 50
– Claudio
If Voce wants to take between one ID and another, use BETWEEN, that is, elq means between then Voce will indicate the ID from 1 to 1000, just use Update name of the FROM fields table name WHERE id BETWEEN 1 and 1000
– Victor
The ids are chosen according to a attendance list where the students present are marked among all of the list so would have to do dynamically so q only take the marked ones and pass to give the update
– Claudio
I’m not getting the form data right, can someone help. //HERE I WILL CHECK BOX ON STUDENTS WHO WANT TO CHANGE TO 1 (PRESENT) Status 1= present, 2= absent <input type='checkbox' name='present' value='1'>Mark Gift?
– Claudio