Update table field only marked with php mysql checkbox

Asked

Viewed 442 times

2

@Leo Caracciolo worked out the way you did in this post here, 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 id_course column, is different for each user id, then each time I use the script and frame a student who has more than one course the script updates all lines of the other courses also where the user id is equal

1 answer

2


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

  • 1

    @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

  • leave your contact to chat, I’m starting to study php in depth and with these tips I’ll take off kkk

  • Good morning Leo, could you explain to me the logic of the archives.

  • @Claudio I hope the explanation is easy to understand :)

  • Perfect was worth!!!!!

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.