Popular table with HTML table data

1

I have a table with 4 columns (name, proof1, proof2, dummy). Suppose you are a teacher, and you want to post student grades. I'll give you the following:

// $l representará o número de linhas que existem
$l = -1;

// $inst2 carrega: SELECT nome, prova1, prova2, simulado FROM alunos

while($dado = mysqli_fetch_row($inst2)) {

      $l+=1;
      $dados[] = $dado;

}
for ($x=0; $x<=$l; $x++) {
 echo "<tr>";
 for ($y=0; $y<=3; $y++) { 
       if ($y==0) {
       echo "<td>".$dados[$x][$y]."</td>";
       } else {       
       echo "<td><input type=\"text\" value=\"".$dados[$x][$y]."\" size=\"6\"></td>";
       }
 }
}
echo "</tr>";
echo "</table>

In other words, the table notes in which there are no registered values (they are null) will have a input for the teacher to insert the student's note.

My problem is: I want to have a button so that after the teacher inserts the notes he clicks on them and they are saved in the database.

I even thought of doing this: put the table that appears on the screen inside a form, and put in inputs text a name using the variables $x and $y . And add a column with an id for each row ( auto_increment ). However, I could not move forward. Even though I believe there is a better way to do that.

PS: I prefer your help using procedural. Thanks!

    
asked by anonymous 31.10.2014 / 21:43

1 answer

-1

<!DOCTYPE html>

<html>
    <?php
    require("header.php");

    //iniciar a pesquisa no banco de Dados para povoar a tabela
    //criar a conexão com o banco de dados
    $link = new mysqli('localhost', 'root', '', 'escola');

    if (!$link) {
        die('Connect Error (' . mysqli_connect_errno() . ') '
                . mysqli_connect_error());
    }
    //Selecionar todos os registros da tabela
    $query = "SELECT * FROM alunos" or die("Error in the consult.." . mysqli_error($link));
    $result = $link->query($query);
    $cont=0;
    ?>
    <body>

        <form method="POST" action="gravarNotas.php">
            <table>
                <thead>
                    <tr>
                        <th>Nome</th>
                        <th>prova1</th>
                        <th>prova2</th>
                        <th>simulado</th>
                    </tr>
                </thead>
                <tbody>

                    <!-- iniciando loop para povoar a tabela --> 
<?php
//Criando variável para armazenar os dados
//Adiciona o ID para poder fazer a gravação no banco posteriormente
$tabela = "Notas";
if ($result) {
    while ($row = mysqli_fetch_array($result)) {
        $id = $row['idalunos'];
        //Saber quantos resgistros existem na tabela
        $cont++;
        
        $tabela .= " <tr>"
                . "<td><input type='text' name='nome_$id' id= '{nome_$id}' value='{$row['nome']}' ></td>
                   <td><input type='text' name='nota1_$id' value='{$row['nota1']}' id= '{nota1_$id}'></td>
                   <td><input type='text' name='nota2_$id' value='{$row['nota2']}' id= '{nota2_$id}'></td>
                   <td><input type='text' name='simulado_$id' value='{$row['simulado']}' id= '{simulado_$id}' ></td> "
                . "</tr>";
    }
    
}

echo $tabela;
?>




                </tbody>
            </table>
            <!-- passando para o post a qtd de registros -->
            <input type="hidden" name="totalRegistros" value="<?=$cont;?>">
            <button type="submit">Enviar notas</button>
        </form>
    </body>

</html>

Here you generated the html and inserted the records into the table. When you click "Send Notes", the "action" action will be performed. So the page to be loaded will be to writeNotes.php. This page will record to the bank.

<?php
  $link = new mysqli('localhost', 'root', '', 'escola');

    if (!$link) {
        die('Connect Error (' . mysqli_connect_errno() . ') '
                . mysqli_connect_error());
    }
     
    if($_POST && $_POST['totalRegistros'] > 0){
         for($i=1; $i<=$_POST['totalRegistros']; $i++ ){
             
             #apenas para ficar mais compreensível
             $nome = $_POST['nome_'.$i];
             $nota1 = $_POST['nota1_'.$i];
             $nota2 = $_POST['nota2_'.$i];
             $simulado = $_POST['simulado_'.$i];
             
             #executa a gravação no bando de dados
              $query = "UPDATE 'escola'.'alunos' SET 'nome'=' {$nome}', 'nota1'= '{$nota1}', 
                  'nota2'= '{$nota2}', 'simulado'= '{$simulado}' WHERE 'idalunos'='{$i}'";
;             $result = $link->query($query);
         }
    }
    header('Location: index.php');
    
31.10.2014 / 22:07