-1

I have a table for students, courses and an intermediate table students/courses.
The problem is, when I edit an user in the admin panel, I wanted it to print checked checkboxes according to the courses that that student is in and leave the others as default (not checked). This is a snippet from my current code:

<?php
     $cursos = getCursos($link);
     $edit = $_GET['edit'];
                            
     foreach ($cursos as $curso) {
         $sql  = "SELECT cur_id FROM tbl_aluno_curso WHERE aluno_id = $edit LIMIT 1";
         $exec = mysqli_query($link, $sql);

               while ($row2 = mysqli_fetch_array($exec)) {
                    extract($row2);    
                     
                    if ($curso['cur_id'] == $cur_id) {
                         echo "
                            <div class='form-check form-check-inline'>
                            <input class='form-check-input' name='curso[]' type='checkbox'
                                        value=".$curso['cur_id']." checked>
                                        <label class='form-check-label'>". $curso['cur_nome'] ."</label>
                                    </div>
                                    ";
                                } else {
                                    echo "
                                    <div class='form-check form-check-inline'>
                                        <input class='form-check-input' name='curso[]' type='checkbox'
                                        value=".$curso['cur_id'].">
                                        <label class='form-check-label'>". $curso['cur_nome'] ."</label>
                                    </div>
                                    ";
                                }
                    }
       }
?>

I first get the courses on the database and then if the course id on the checkboxes it's equal to the id on the database it prints the checkbox checked, if not, it prints the checkbox unchecked.
The code is working fine as long as the student has only one course. If the student has, for example 2 courses, it prints 10 checkboxes insthead of 5 (which is the number of courses available). This happens because the query returns 2 rows as the student is in 2 courses. I want it to return the courses at the same time for the checkboxes. Any ideas on how to solve this?
Example of the problem: The student has 2 courses which are guitar and piano. When i edit the used it prints 2 sets of 5 checkboxes. The first set has the guitar checkbox checked and the second set has the piano checkbox checked.

neptune17
  • 19
  • 6
  • **Warning:** You are wide open to [SQL Injections](https://php.net/manual/en/security.database.sql-injection.php) and should use parameterized **prepared statements** instead of manually building your queries. They are provided by [PDO](https://php.net/manual/pdo.prepared-statements.php) or by [MySQLi](https://php.net/manual/mysqli.quickstart.prepared-statements.php). Never trust any kind of input! Even when your queries are executed only by trusted users, [you are still in risk of corrupting your data](http://bobby-tables.com/). [Escaping is not enough!](https://stackoverflow.com/q/32391315) – Dharman Mar 11 '22 at 20:02
  • could you please check my answer to your question and give a feedback? – Selim Acar Mar 14 '22 at 09:37
  • 1
    Alright, it worked! – neptune17 Mar 15 '22 at 09:53

1 Answers1

1

This code would do what you need. Please mark this answer as ACCEPTED if it works for you.

// Get all courses
$cursos = getCursos($link);


// Get courses of the student and prepare a list of course ids
$alunoId= intval($_GET['edit']);
$sql  = "SELECT cur_id FROM tbl_aluno_curso WHERE aluno_id = $alunoId";
$exec = mysqli_query($link, $sql);
$cursosDeAluno = [];
while ($row = mysqli_fetch_array($exec))
{
    $cursosDeAluno[] = $row['cur_id'];
}

// prepare courses list with some of them checked
$cursosListAsHtml = "";
foreach ($cursos as $curso)
{
    extract($curso);
    $checked = in_array($cur_id, $cursosDeAluno) ? " checked " : "";
    $cursosListAsHtml .= "\r\n
    <div class='form-check form-check-inline'>
        <input class='form-check-input' name='curso[]' type='checkbox' value=\"$cur_id\" $checked>
        <label class='form-check-label'>$cur_name</label>
    </div>\r\n";
}

echo $cursosListAsHtml;
Selim Acar
  • 378
  • 2
  • 9