Update only the records marked with a checkbox at once php

3

I have a select that brings a series of records. Here, in these records I need to apply an up and that in my head, the best way would be to use a checkbox field, so that when the user marks the checkbox and then clicks the "submit" button, only the field of the records - would be updated at the same time (with 1 or 2 depending on whether the field was marked 1 or not 2). Espe .. Thanks for the help! I'm breaking my head here and I can never get it searched and asked and I did not get conclusive answers.

form.php

<?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'>&nbsp;</td>
        <td valign='middle'>&nbsp;</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'>
<input type='checkbox' name='presente' value='2'>
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?
    
asked by anonymous 07.11.2017 / 00:58

1 answer

1

As I understand it, this is a list of the students' presence.

Suggestion for this first solution: in the column column presente as Default Value equal to 2, then it would only be necessary to make update for those present, all that is needed is a checkbox that would be marked if the student is present.

The values of the checkboxes are the id_al from the query that will be used in the where clause in the update of the presente column with the value 1 of checked checkboxes.

form.php

<?php
$conn = new mysqli("localhost", "USUARIO", "SENHA", "NOME_DB");
$idEvent = $_POST['idsubev'];

$sql = "SELECT ....................
$query = mysqli_query($conn, $sql);

echo "<form method='post' action = 'Update.php' >

<h1> Alterar presença do aluno</h1>
<table align='' border='0' bordercolor='#BCBCBC' cellspacing='0'>
<tr align ='left' bordercolor='#000000' >
    <td valign='middle'>&nbsp;</td>
    <td valign='middle'>&nbsp;</td>
</tr>";


while ($rows = mysqli_fetch_assoc($query)) {

    echo "<tr align ='left' bordercolor='#000000' ><td valign='middle' bgcolor='#E9E9E9'><p><font color=''>Nome:</font> </p></td>
        <td align='left' valign='middle' bgcolor='#E9E9E9'>".$rows['nome']."</td>
    </tr>

    <tr><td><font color=''> Curso: </font> </td>
        <td align='left'>".$rows['titulo']."</td>
    </tr>
    <tr align ='left'>
        <td><font color=''>Presente=<b>presente</b> </font></td>
        <td align='left'>
            Status marcado = presente <input type='checkbox' name='presente[]' value='".$rows['id_al']."'>Marcar Presente?
        </td>
     </tr>";

}

echo "</table>
<input type='submit' value='alterar'>
</form>";

?>

Update.php

<?php

$presente = $_POST['presente'];

$conn = new mysqli("localhost", "USUARIO", "SENHA", "NOME_DB");

$sql = 'UPDATE sch_aluno_acont SET presente = 1 WHERE id_al IN (' . implode(',', array_map('intval', $presente)) . ')';
$data = mysqli_query($conn, $sql);

?>

To update all checked or unmarked checkboxes

In this solution there is no need for column presente to be Default Value

form.php

.............................
.............................
while ($rows = mysqli_fetch_assoc($data)) {

    echo "<tr align ='left' bordercolor='#000000' ><td valign='middle' bgcolor='#E9E9E9'><p><font color=''>Nome:</font> </p></td>
        <td align='left' valign='middle' bgcolor='#E9E9E9'>".$rows['nome']."</td>
    </tr>

    <tr><td><font color=''> Curso: </font> </td>
        <td align='left'>".$rows['titulo']."</td>
    </tr>
    <tr align ='left'>
        <td><font color=''>Presente=<b>presente</b> </font></td>
        <td align='left'>
            Status marcado = presente <input type='checkbox' name='presente[]' value='".$rows['id_al']."'>
Marcar Presente?
        </td>
     </tr>";
 //todos os id_al 
 $all_id_al .=$rows['id_al'].",";   
}

echo "<input type='hidden' name='all_id_al' value='".substr($all_id_al, 0, -1)."'>";

echo "</table><input type='submit' value='alterar'></form>";

?>

Update.php

<?php

$presente = $_POST['presente'];

$all_id_al = $_POST['all_id_al'];

$all_id = explode(",",$all_id_al);

$result=array_diff($all_id,$presente);

$conn = new mysqli("localhost", "USUARIO", "SENHA", "NOME_DB");

//marcados
$sql = 'UPDATE sch_aluno_acont SET presente = 1 WHERE id_al IN (' . implode(',', array_map('intval', $presente)) . ')';
$data = mysqli_query($conn, $sql);

//não marcados
$sql2 = 'UPDATE sch_aluno_acont SET presente = 2 WHERE id_al IN (' . implode(',', array_map('intval', $result)) . ')';
$data2 = mysqli_query($conn, $sql2);

?>
  

of the form comes a string with all id_al that we transform into an array $all_id .

     

$result=array_diff($all_id,$presente); returns the values of $all_id that are not present in $presente and that will be used to update unchecked checkboxes.

    
07.11.2017 / 04:07