php - How to insert multiple rows of student records scores in a while loop in a database table? -
i want create table input fields student records can inserted. name of students in first column of table fetched database while loop. other columns contain fields inputing student scores. challenge i'm facing how insert records of students in different row of table called result_sec database. i've search similar post couldn't suitable answer. below code. in advance.
<?php require('header.php'); ?> <?php $query_form = sprintf("select * regform limit 2"); $form = mysqli_query($conn, $query_form) or die(mysqli_error($conn)); $formdata = mysqli_fetch_assoc($form); if(isset($_post['submit'])) { $exes = $_post['exe']; $asss = $_post['ass']; $ca1s = $_post['ca1']; $ca2s = $_post['ca2']; $exams = $_post['exam']; foreach($exes $key => $exe) { $sql = "insert result_sec (exe, ass, ca1, ca2, exam) values ('$exe', '$asss[$key]', '$ca1s[$key]', '$ca2s[$key]', '$exams[$key]')"; } $insert = mysqli_multi_query($conn, $sql); } ?> <form method="post"> <table> <thead> <tr> <th>name</th> <th>ass.</th> <th>exe.</th> <th>1st c.a.</th> <th>2nd c.a.</th> <th>exam</th> </tr> </thead> <tbody> <?php { ?> <tr> <td><?php echo $formdata['surname']." ".$formdata['firstname']; ?></td> <td><input name="ass[]" size="1px"/></td> <td><input name="exe[]" size="1px" /></td> <td><input name="ca1[]" size="1px" /></td> <td><input name="ca2[]" size="1px" /></td> <td><input name="exam[]" size="1px" /></td> <input type="hidden" name="regformid[]" value="<?php echo $formdata['regformid'];?>" /> </tr> <?php } while ($formdata = mysqli_fetch_assoc($form)); ?> </tbody> </table> <button type="submit">insert student record</button> </form> <?php require('footer.php'); ?>
see if resolve problem
if(isset($_post['submit'])){ $exes = $_post['exe']; $asss = $_post['ass']; $ca1s = $_post['ca1']; $ca2s = $_post['ca2']; $exams = $_post['exam']; //you can use foreach loop loop on 1 of repeated inputs, , use index access corresponding elements in others: foreach ($exes $i => $exe) { $exee = mysqli_real_escape_string($exe); $ass = mysqli_real_escape_string($asss[$i]); $ca1 = mysqli_real_escape_string($ca1s[$i]); $ca2 = mysqli_real_escape_string($ca2s[$i]); $exam = mysqli_real_escape_string($exams[$i]); $sql = "insert result_sec (exe, ass, ca1, ca2, exam) values ('$exee', '$ass', '$ca1', '$ca2', '$exam')"; $insert = mysqli_multi_query($conn, $sql); } }
Comments
Post a Comment