Insert multiple rows into a MySQL database from a table

后端 未结 1 1793
耶瑟儿~
耶瑟儿~ 2021-01-24 03:09

I retrieve data from databased based on 4 dropdown criteria.Display them on a table and add one column for scores of each student. I want to populate another table with all the

相关标签:
1条回答
  • 2021-01-24 03:32

    I have found an answer to the question: Just declare the name field as an array and insert them into the DB using foreach loop.

    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); 
           }
    
         }
    
    0 讨论(0)
提交回复
热议问题