Visualizzazione dei risultati da 1 a 4 su 4
  1. #1

    Inserire più record in una tabella

    salve a tutti
    ho due tabelle:
    STUDENTI (id, codice, nome)
    VOTI (id, codice, nome, voto)
    con un primo script mi estraggo tutti gli studenti e li stampo con accanto la casella del voto:
    Codice PHP:
     $query = "SELECT * FROM studenti ORDER by stu asc"; 
    $result = mysql_query($query); 
    //$numero = mysql_num_rows($result);
    while ($row = mysql_fetch_array($result))
    {
    ?>
    <form action="studenti_salva_voti.php" method="post" name="form1" id="form1">

    <table width="800" border="0" align="center"  cellpadding="0" cellspacing="0" bgcolor="#FF0000">
     <tr>
    <td width="5%"> </td>
    <td width="85%">
    <input name="codice" type="text" value="<?php print $row['stu'] ; ?> " />
    <input name="nome" type="text" value="<?php print $row['nome'] ; ?> " />
    <input name="voto" type="text" />
    </td>
    </tr>
    </table>
    <?php ?>
    <input name="submit" type="submit" value="submit" /></form>
    quello che voglio fare è salvare tutti gli studenti (codice, nome e voto) nella tabella voti
    grazie

  2. #2
    Utente di HTML.it L'avatar di boots
    Registrato dal
    Oct 2012
    Messaggi
    1,626
    prova così:

    Codice PHP:
    <?php
     $query 
    "SELECT * FROM studenti ORDER by stu asc";  
    $result mysql_query($query);  
    //$numero = mysql_num_rows($result);
    ?> 
    // QUESTA va fuori da while altrimenti ti stampa N volte <form>
    <form action="studenti_salva_voti.php" method="post" name="form1" id="form1"> 
    <?php
    $i
    =0;
    while (
    $row mysql_fetch_array($result)) 

    ?> 
    <table width="800" border="0" align="center"  cellpadding="0" cellspacing="0" bgcolor="#FF0000"> 
     <tr> 
    <td width="5%"> </td> 
    <td width="85%"> 
    <input name="data[<?= $i ?>][codice]" type="text" value="<?php print $row['stu'] ; ?> " /> 
    <input name="data[<?= $i ?>][nome]" type="text" value="<?php print $row['nome'] ; ?> " /> 
    <input name="data[<?= $i++ ?>][voto]" type="text" /> 
    </td> 
    </tr> 
    </table> 
    <?php ?> 
    <input name="submit" type="submit" value="submit" /></form>
    Nella pagina studenti_salva_voti.php dovresti avere una array con tutti gli studenti:

    Codice PHP:
    $data $_POST['data'];
    foreach(
    $data as $line){
          echo 
    $line['codice']." "$line['nome']." ".$line['voto']."
    "
    ;
          
    // Qui puoi fare l'inserimento nel db


  3. #3
    Grazie per l'aiuto
    ho fatto come mi hai detto e a video mi stampa tutti e 4 i record ma mi salva solo l'ultimo


    Codice PHP:
    <?php
    $data 
    $_POST['data'];
    foreach(
    $data as $line){
          echo 
    $line['codice']." "$line['nome']." ".$line['voto']."
    "
    ;
          
    // Qui puoi fare l'inserimento nel db
    }



    mysql_query("insert into voti (stu, nome, voto) 
    VALUES ('
    $line[codice]','$line[nome]','$line[voto]')");

    ?>

  4. #4
    RISOLTO
    non avevo spostato la parentesi graffa

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.