I have a system that inserts student marks into the database. When a user enters the marks, he/she enters the marks for many students (minimum 30), (in a form format) so the form has the student name label and an input field for entering the mark.
My challenge is that when only one student is entered into the database. The rest do not get inserted
How can I when I press the add button, have all the students I've entered marks for, be inserted into the database.
Below is my code. The HTML /Form code
while($row = $marks_query->fetch_assoc()) {
$student_id=$row["student_id"];
$student_name=$row['student_lastname'].' '.$row['student_midname'].' '.$row['student_firstname'];
echo '<tr>
<td class="align-middle">' . $student_name.'</td>';
?>
<td class="align-middle"><input type="number" class="form-control marks" placeholder="Enter Marks" name="marks" ></td>
<?php
echo '</tr>';
}
?>
<input type="hidden" name="class_list" id="class_list" value="<?php echo $class_list; ?>" >
<input type="hidden" name="subject_list" id="subject_list" value="<?php echo $subject_list; ?>" >
<input type="hidden" name="teacher_id" id="teacher_id" value="<?php echo $teacher_id; ?>" >
<input type="hidden" name="assessment" id="assessment" value="<?php echo $assessment; ?>" >
<input type="hidden" name="student_id" id="student_id" value="<?php echo $student_id; ?>" >
</tbody>
</table>
<button class="btn btn-outline-primary col-md-auto float-right" name="add_marks" id="add_marks" onclick="addMarks();">Add Student Marks</button>
<script type="text/javascript">
<script type="text/javascript">
function addMarks(){
var class_list = "<?php echo $class_list; ?>";
var student_id = "<?php echo $student_id; ?>";
var subject_list = "<?php echo $subject_list; ?>";
var teacher_id = "<?php echo $teacher_id; ?>";
var assessment = "<?php echo $assessment; ?>";
var marks=$("#marks").val();
$.ajax({
type:"POST",
url:"insert_marks.php",
data: { class_list: class_list, subject_list: subject_list, student_id: student_id,teacher_id:teacher_id,assessment:assessment,marks:marks },
success:function(data){
}
});
}
</script>
Here is the PHP CODE I'm still going to sanitize and the code and use Prepared statements to prevent SQL Injection
$assessment=$_POST['assessment'];
$class_list=$_POST['class_list'];
$subject_list=$_POST['subject_list'];
$marks=$_POST['marks'];
$teacher_id=$_POST['teacher_id'];
$student_id=$_POST['student_id'];
$insert_sql=mysqli_query($conn,"INSERT INTO `aza_assesmentxmarks`(`id`, `student_id`, `teacher_id`, `assessement_id`, `subject_id`, `class_id`, `mark`, `date`) VALUES (NULL,'$student_id','$teacher_id','$assessment','$subject_list','$class_list','$marks',NOW())") or die(mysqli_error());
echo $marks;
?>