Update table field just checked with checkbox php mysql

3

@Leo Caracciolo worked out the way you did # , but I have another problem: there are students who are enrolled in several courses, ie the student id is the same but the course id in the run_id column is different for each user id, so each time I use the script and mark a student who has more than one course the script updates every row of the other courses also where id of the user is equal

    
asked by anonymous 08.11.2017 / 00:06

1 answer

2

form.php

<?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 logic

In the page form.php we enter in the value of input type='checkbox' the values returned from the query id_al and id_acon separated by an asteristico (*).

<input type='checkbox' name='presente[]' value='".$rows['id_al']."*".$rows['id_acon']."'>";

Notice that no input name was added by two brackets [ ]

name='presente[]'

When you put a "name" with square brackets it is sent in array form to the receiver.

On page Update.php

We retrieve the data coming from the form (array) $presente = $_POST['presente'];

With foreach ($presente as $value) { , we have that at each iteration, the value of the current element is assigned to $ value, for example 100244*171007074752

In possession of this value we make a explode and put the appropriate parts in the condition where of update WHERE id_al='$parte[0]' and id_acon='$parte[1]'"

  

A correction was made to the file form.php so that the output in html is correct.

    
08.11.2017 / 00:16