Insert data from array into mysql table using php
Teacher will use this form insert students marks or grades.
<form action="action.php" method="post" enctype="multipart/form-data"> <?php $result2 = mysqli_query($conn,$query2); $subjects_array = array(); if (mysqli_num_rows($result2)>0){ while ($row2=mysqli_fetch_assoc($result2)){ $subjects_array[] = $row2; } } ?> <div class="col-md-12"> <?php //print_r($subjects_array); foreach ($subjects_array as $subject) { echo "<div class='col-md-4'hidden>" .$studentid ." " .$studentgrade ." " .$studentgradetype ." " .$subject['sub_id'] ." " ."</div>".$subject['name'] .":" ."<div> <input type='number' min='1' max='100' class='form-control col-md-4' placeholder='Enter marks' size='10' name='marks[]'> </div>";} ?> <button type="submit" class="btn btn-primary" name="add_result" >Add Results</button> <button type="submit" class="btn btn-danger" name="backtomanageresults">Cancel</button> </form>
here is my not working at all insert code
<?php if (isset($_POST['add__result'])){ $marks = $_POST['marks[]']; foreach ($subjects_array as $subject) { $query = "INSERT INTO results (`student_id`, `student_grade`, `student_gradetype`, `student_subjects`, `student_results`) VALUES (`.$studentid` ,`.$studentgrade`, `.$studentgradetype`, `.$subject['name']`, `.$marks`)"; $stmt=$conn->prepare($query); $stmt->execute();} header('location:manage_results.php?resultadded=success'); } ?>
I want to insert all of this ($studentid, $studentsgrade, $studentgradetype, $subject[‘sub_id’], $subject [‘name’] and marks[]) into one table in mysql table named ‘results’. I tried so many methods but still get errors. I know very well that am not going in the right way with my inser code that why i need help on this.