简体   繁体   English

使用 WHILE 循环向数据库添加多行

[英]Adding multiple rows to database using WHILE loop

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.当用户输入分数时,他/她会为许多学生(至少 30 个)输入分数(以表格格式),因此表格具有学生姓名标签和用于输入分数的输入字段。

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 HTML/表单代码

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这是我仍然要清理的 PHP 代码和代码并使用准备好的语句来防止 SQL 注入




$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;
?>


If you send all of the data as strings it's going to only create 1 record in the db.如果您将所有数据作为字符串发送,它只会在数据库中创建 1 条记录。 You should send all of the data as arrays and then iterate through the arrays with a for loop and then send the data from the arrays to the db on each cycle of the for loop.你应该将所有的数据作为数组,然后迭代通过与阵列for循环,然后从阵列中的数据发送到数据库上的每个周期for循环。 If you need any help, ask here.如果您需要任何帮助,请在此处询问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM