George Hermsilla
George Hermsilla

Reputation: 9

How to save a php array in a mysql table?

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

Upvotes: 0

Views: 81

Answers (2)

Dan Hawkins
Dan Hawkins

Reputation: 71

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?

Upvotes: -1

Barmar
Barmar

Reputation: 780724

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) {
    ...
}

Upvotes: 2

Related Questions