php form.
<?php
$conn = new mysqli("localhost", "USUARIO", "SENHA", "NOME_DB");
$query = "SELECT * FROM sch_aluno_acont";
$data = mysqli_query($conn, $query);
echo "<form method='post' action = 'update2.php' >
<h1> Alterar presença do aluno</h1>
<table align='' border='0' bordercolor='#BCBCBC' cellspacing='0'>
<tr align ='left' bordercolor='#000000'>
<td colspan='2' valign='middle'><font color=''>Marque o(s) curso(s) para <b>presença</b> </font><br></td>
</tr>";
$id_al_Old="zzz";
while ($rows = mysqli_fetch_assoc($data)) {
if ($rows['id_al']!=$id_al_Old) {
if ($id_al_Old!="zzz") {
echo "</td>";
echo "</tr>";
}
echo "<tr align ='left' bordercolor='#000000'>
<td valign='middle' bgcolor='#E9E9E9'><p><font color=''>Nome:</font>".$rows['nome']." </p></td>
<td align='left' valign='middle' bgcolor='#E9E9E9'>".$rows['titulo']."</td>
</tr>
<tr align ='left'>
<td colspan='2' align='left'>";
}
echo "curso ".$rows['id_acon']."
<input type='checkbox' name='presente[]' value='".$rows['id_al']."*".$rows['id_acon']."'>";
$id_al_Old=$rows['id_al'];
}
echo "</td></tr></table><input type='submit' value='alterar'></form>";
?>
Update.php
<?php
$presente = $_POST['presente'];
$conn = new mysqli("localhost", "USUARIO", "SENHA", "NOME_DB");
foreach ($presente as $value) {
$parte=explode("*",$value);
$sql = "UPDATE sch_aluno_acont SET presente = 1 WHERE id_al='$parte[0]' and id_acon='$parte[1]'";
$data = mysqli_query($conn, $sql);
}
?>
Explaining the logic
On the page form.php
enter in the value of input type='checkbox'
the values returned from the id_al
and id_acon
separated by an asterisk (*).
<input type='checkbox' name='presente[]' value='".$rows['id_al']."*".$rows['id_acon']."'>";
Note that in the input name two brackets have been added [ ]
name='presente[]'
When you place a bracketed "name" it is sent as an array to the receiver.
On the page Update.php
We rescued the data from the form (array) $presente = $_POST['presente'];
With foreach ($presente as $value) {
, we have to each iteration, the value of the current element is assigned to $value, for example 100244*171007074752
of possession of that value we make a explode
and put the proper parts in the condition where
of the update WHERE id_al='$parte[0]' and id_acon='$parte[1]'"
a correction was made to the file form.php
so that the exit in html
be correct.
Perfect guy This is great friend, I don’t know how to thank you, if possible explain to me the logic why I was floating in it.... strong hug and thank you so much
– Claudio
@Claudio need not thank you, mark the answers as accepted see how and why https://pt.meta.stackoverflow.com/questions/1078/como-e-por-que-aceitar-uma-resposta/1079#1079
– user60252
leave your contact to chat, I’m starting to study php in depth and with these tips I’ll take off kkk
– Claudio
Good morning Leo, could you explain to me the logic of the archives.
– Claudio
@Claudio I hope the explanation is easy to understand :)
– user60252
Perfect was worth!!!!!
– Claudio