dongshou7903 2017-06-03 00:21
浏览 38

如何在mysql表中保存php数组?

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条回答 默认 最新

  • douyao6842 2017-06-03 00:30
    关注

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

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效