0

this is my page

notes.php

pageview

And this is the code I have, what it does is select the students and print their notes.

 Select notes to add
        <select id="combito">
            <option>Notas</option>
            <option value="1">1</option>
            <option value="2">2</option>
            <option value="3">3</option>
            <option value="4">4</option>
            <option value="5">5</option>
        </select>
        Select the module:
        <select id="combito2">
            <option>Materia</option>
            <option value="lenguaje">History</option>
            <option value="matematicas">Math</option>
        </select>
        <div id="div_1" class="contenido">
            <table>
<thead>
<tr>
<td>RUT</td>
<td>Nombre</td>
<td>Apellido</td>
<td>Notas</td>
</tr>
</thead>
<tbody>
<?php

$connect = mysqli_connect("localhost","root", "","liceo");
if (!$connect) {
die(mysqli_error());
}
$results = mysqli_query($connect,"SELECT rut, nombre, apellido FROM alumnos");
while($row = mysqli_fetch_object($results)) {
$rut = $row->rut;
$boo = 0; /* Iniciamos la Variable Auxiliar
          que indicará cuantas notas se imprimió  por cada Alumno  */
$results2 = mysqli_query($connect,"SELECT nota FROM notas WHERE rut_alumno = '$rut' AND id_materia=1 LIMIT 1");
?>
<tr>
<td><?=$row->rut?></td>
<td><?=$row->nombre?></td>
<td><?=$row->apellido?></td>
<td>
<?php
while($nota = mysqli_fetch_object($results2)):
?>
 <input type="text" name="pin" maxlength="2" size="2" value="<?=$nota->nota?>">
<?php
    $boo +=1;/* Incrementamos después de Imprimir la nota del Alumno*/
endwhile;
/* Si la variable es menor a 2 , es decir no se imprimieron las 2 notas respectivas*/
if($boo<1){ 
    /* Imprimimos  inputs de value 0 hasta que sea < 2 , dado que si el
    el valor de $boo es 1 o 0 , primero se realizará el echo y luego el incremento
    Sí $boo es 0 -> Iteración 0 - Imprime el input - Incrementa $boo -> $boo = 1
                    Iteración 1 - Imprime el input - Incrementa $boo -> $boo = 2
                    Termina el for dado que 2 no es menor que 2
    Sí $boo es 1 -> Iteración 0 - Imprime el input - Incrementa $boo -> $boo = 2
                    Termina el For dado que 2 no es menor que 2
     */
    for (; $boo < 1; $boo++) { 
        echo '<input type="text" name="pin" maxlength="2" size="2" value="10">';
    }
}
?>
</td>
</tr>
<?php
  }
?>
</tbody>
</table>
 <input type="button" value="Save notes">
        </div>

The problem is that I do not know how to save all the "inputs" that are generated. For example if I have 11 names there will be 11 inputs generated, and if there are 12 students there will be 12 inputs that will be generated as well. Then I would like to know how to save the value of those notes, in my table.

This is my table

notas

view table

I appreciate if can guide me

2
  • Why is there only one input in the Notas column? Your code has a while loop that should put an input for every row returned by the query. If the query only returns one row, why are you using a loop? Commented Jun 3, 2017 at 0:31
  • You have LIMIT 1 in the second query. Why do you need a while loop to fetch just one row of results? Commented Jun 3, 2017 at 21:13

2 Answers 2

2

You need to use [] in the input name. Then PHP will create an array of all the inputs. So it should be:

<input type="text" name="pin[<?=$rut?>]" maxlength="2" size="2" value="<?=$nota->nota?>">

Then the script that processes the form can loop through all the values in $_POST['pin']:

foreach($_POST['pin'] as $rut => $pin) {
    ...
}
Sign up to request clarification or add additional context in comments.

6 Comments

Thnks, in the script i add echo $pin; but why only give me back two values, the value of the first and last note?
Is $rut different for each note?
$rut is the student ID, can have many notes. refers to the student
But you only have one note for each student in the form, because you do LIMIT 1 in the query.
I had not seen him, Ty. I have fixed the query, but it also prints me 2 values. The first note entered and the last note, and not all notes in inputs.
|
-1

Each input will need a unique name, like pin_1, pin_2 and etc. When you submit the form, you can step through each input and insert the data.

Like foreach ( $_REQUEST as $key => $value ) {

Does that help?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.