Mysql
 sql >> Database >  >> RDS >> Mysql

Inserisci più righe in un database MySQL da una tabella

Ho trovato una risposta alla domanda:basta dichiarare il campo del nome come un array e inserirli nel DB usando il ciclo foreach.

while($fetch=mysqli_fetch_array($confirm)){
        $c++;
    echo "<tr>";
    echo "<td>".$c."</td>";
    echo"<td><input type='text' name='admNo[]' value='".$fetch["AdmNo"]."'></td>";
    echo"<td><input type='text' name='sname[]' value='".$fetch["Surname"]."'></td>";
    echo"<td><input type='text' name='fname[]' value='".$fetch["Firstname"]."'></td>";
    echo"<td><input type='text' name='class[]' value='".$fetch["class"]."'></td>";
    echo"<td><input type='text' name='SessionAssigned[]' value='".$fetch["CSession"]."'></td>";
    echo"<td><input type='text' name='Term[]' value='".$fetch["Term"]."'></td>";
    echo "<td><input type='text' name='sub[]' value='".$sub."'</td>";
    echo "<td><input type='text' name='score[]'></td>";
    echo "</tr>";   
    }
  //if score is  supplied , then click to save to database
     }
   if(isset($_POST["saveBtn"])){

       foreach($_POST["admNo"] as $rec=> $value){
         $cl = $_POST["class"][$rec];
         $term = $_POST["Term"][$rec];
         $ad = $_POST["admNo"][$rec];
         $Csess = $_POST["SessionAssigned"][$rec];
         $sub = $_POST["sub"][$rec];
         $sc = $_POST["score"][$rec];  

  $insert = "INSERT INTO result_tab(CSession,Term,Class,AdmNo,subject,score)VALUES('$Csess','$term',
                '$cl','$ad','$sub','$sc')";
 $succ = mysqli_query($connection,$insert); 
       }

     }