Block entries beyond the value stipulated in PHP form

Asked

Viewed 83 times

1

I have a course administration system where the teacher registers a class and the student accesses a page with a table with all classes and enrolls in what he thinks best.

The problem is that many students are enrolling in a class that should behave much less, so what I wanted to do:

The teacher stipulates the number of students who can participate, there at the time a student makes his registration in that class, compares the number of students allowed by the teacher with the number of students already registered, if still has vacancy, the registration is saved, if not, an Alert appears and goes back to the previous page.

This is the page the student accesses with the table that shows the existing classes:

<?php
//Conexão e consulta ao Mysql
mysql_connect('localhost','root','') or die(mysql_error());
mysql_select_db('db_qualquer') or die(mysql_error());
$qry = mysql_query("select * from turmas");

//Pegando os nomes dos campos
$num_fields = mysql_num_fields($qry);//Obtém o número de campos do resultado

for($i = 0;$i<$num_fields; $i++){//Pega o nome dos campos
    $fields[] = mysql_field_name($qry,$i);
}

//Montando o cabeçalho da tabela
$table = '<table class="table table-hover table-inverse" style="margin-top:50;background-color: #37444a; color:lightgrey;"> <tr>';

for($i = 0;$i < $num_fields; $i++){
    $table .= '<th>'.$fields[$i].'</th>';
}

//Montando o corpo da tabela
$table .= '<tbody style="
    background-color: #86979e;
    color: #37444a;    
">';
while($r = mysql_fetch_array($qry)){
    $table .= '<tr>';
    for($i = 0;$i < $num_fields; $i++){
        $table .= '<td>'.$r[$fields[$i]].'</td>';
    }


    $table .= '<td><form action="inscricao.php" method="post">';
    $table .= '<input type="hidden" name="ID" value="'.$r['ID'].'">';
    $table .= '<input type="hidden" name="NOME" value="' . $r["NOME"] . '">';
    $table .= '<button class="btn btn-primary"><i class="glyphicon glyphicon-pencil"> Inscreva-se</i></button>'; 
    $table .= '</form></td>';
}

//Finalizando a tabela
$table .= '</tbody></table>';

//Imprimindo a tabela
echo $table;

?>

This is the sign-up page that appears when the student clicks on the sign-up button:

<div class="container">
    <div class="row">
        <div class="col-lg-12 text-center">
            <h1 style="
                margin-top:100px;">Inscrição</h1>
            <p> </p>
            <p class="lead"></p>
            <ul class="list-unstyled">
                <form id="cadastro" method="post" action="banco/updateP.php" style="
                    text-align: left;
                    margin-top:50px;">
                    <div class="col-lg-12">
                        <div class="form-group" style="
                    text-align: left;">
                            <label  for="FORMACAO">Formação: </label>
                            <input  type="text" required class="form-control" id="FORMACAO" name="FORMACAO" value="<?php echo $formacao; ?>">
                         </div>
                    </div>
                    <div class="col-lg-12">
                        <div class="form-group" method="post" style="
                    text-align: left;">
                            <label  for="TURMA">Turma: </label>
                            <input  type="text" required class="form-control" id="TURMA" name="TURMA" value="<?php echo $turma; ?>">
                         </div>
                    </div>
                    <div class="col-lg-12">
                        <div class="form-group" method="post" style="
                    text-align: left;">
                            <label  for="COLABORADOR">Colaborador: </label>
                            <select  class="form-control" id="COLABORADOR" name="COLABORADOR">
                                <option>Selecione...</option>
                                <?php while($colab = mysqli_fetch_array($queryColaboradores)) { ?> 
                                <option value="<?php echo $colab['NOME']; ?>"><?php echo $colab['NOME']; ?></option>
                                <?php } ?>
                            </select>
                        </div>
                    </div>
                    <div class="col-lg-12">
                        <div class="form-group" method="post" style="
                    text-align: left;">
                            <label  for="AREA">Área: </label>
                            <select  class="form-control" id="AREA" name="AREA">
                                <option> MITV </option>
                                <option> CTCA </option>
                                <option> VSPA </option>
                            </select>
                        </div>
                    </div>
                    <div class="col-lg-12">
                        <div class="form-group" method="post" style="
                    text-align: left;">
                            <label  for="DATA">Data: </label>
                            <select  class="form-control" id="DATA" name="MES">
                                <option> jan </option>
                                <option> fev </option>
                                <option> mar </option>
                                <option> abr </option>
                                <option> mai </option>
                                <option> jun </option>
                                <option> jul </option>
                                <option> ago </option>
                                <option> set </option>
                                <option> out </option>
                                <option> nov </option>
                                <option> dez </option>
                            </select>
                        </div>
                    </div>
                    <div class="col-lg-12">
                        <div class="form-group" method="post" style="
                    text-align: left;">
                            <label  for="PREVISTO">Status: </label>
                            <input  type="text" required class="form-control" id="PREVISTO" name="PREVISTO" value="Previsto">
                         </div>
                        <div class="form-check form-check-inline disabled">
                            <label class="form-check-label">
                                <input class="form-check-input" type="radio" name="REALIZADO" id="REALIZADO" value="Realizado" disabled> Realizado
                            </label>
                        </div>
                        <div class="">
                            <button type="submit" class="btn btn-primary btn-lg btn-block"> <i class="glyphicon glyphicon-floppy-disk"> Salvar </i></button>
                        </div>
                        <div class="alert alert-info" role="alert">
                            <strong>Hey! </strong> Antes de realizar o cadastro, certifique-se de que não se esqueceu de nada! :)
                        </div>
                        <?php 
                            //ESSE É O TESTE QUE EU TENHO FEITO E QUE NÃO TEM FUNCIONADO 

                            $limite = "SELECT * FROM turmas WHERE ID = 'TURMA' and NOME = 'FORMACAO'";

                            $contador = "SELECT COUNT(ID) AS ComparaLIMITE FROM participantes WHERE TURMA = 'turma' and FORMACAO = 'formacao'";
                            if ($contador <= $limite) { header("Location: inscricao.php"); } 
                            else { 
                                echo '<script type="text/javascript">
                                        alert("Essa turma estava completa");
                                        window.history.go(-1);
                                    </script>';
                             }

                            }
                        ?>
                    </div>
                </form>
            </ul>
        </div>
    </div>
</div>

What I have done so far is to add an extra column to my database table and a field in the teacher’s form to stipulate the number of students, the name of the field is LIMIT.

What I want to do is compare the limit with the number of writings on this page with the registration form and the registration only be validated if the limit is less than the number of subscribers.

How do I compare the number of students already enrolled in that class with the amount stipulated by the teacher when you click the button?

  • How you store each student’s enrolment in the course?

  • When the student clicks to register opens a form that is already written the class and the class id to put his information. I saved this form in a table called "participants"

  • 1

    Then just count how many records there are already in the "participants" table for the given course and compare with the "limit" column".

  • Yeah, but how do I do it inside the button so he knows where he redirects?

  • Not inside the button. You can do this on the page that makes the student registration, only allowing to finish when there are vacancies, otherwise redirects the user.

  • WOW, GOOD IDEA! I’ll DO THAT, THANK YOU.

Show 1 more comment
No answers

Browser other questions tagged

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