提问人:Astute Dlamini 提问时间:12/29/2019 更新时间:12/29/2019 访问量:149
使用 WHILE 循环向数据库添加多行
Adding multiple rows to database using WHILE loop
问:
我有一个系统,可以将学生分数插入数据库。当用户输入分数时,他/她会输入许多学生的分数(至少 30 个),(以表单格式),因此表单具有学生姓名标签和用于输入分数的输入字段。
我面临的挑战是,当只有一个学生被输入数据库时。其余的不插入
当我按下添加按钮时,我怎样才能将我输入的所有学生都插入数据库。
下面是我的代码。 HTML /Form 代码
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>
这是PHP代码 我仍然要清理代码并使用 Prepared 语句来防止 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;
?>
答:
0赞
Leon Kunštek
12/29/2019
#1
如果将所有数据作为字符串发送,则只会在数据库中创建 1 条记录。您应该将所有数据作为数组发送,然后使用循环遍历数组,然后在循环的每个周期将数据从数组发送到数据库。如果您需要任何帮助,请在此处询问。for
for
评论
0赞
Leon Kunštek
12/29/2019
您能否提供此代码的输出或预期输出:.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();
0赞
Astute Dlamini
12/29/2019
例如Class_list=1、student_id=STUD_234、subject_list=2、teacher_id=TEACHER_45、评估=2、分数=67
0赞
Leon Kunštek
12/29/2019
每个学生只有 1 个字符串(例如),还是更多(例如)?var student_id="STUD_234"
var student_id="STUD_234, STUD_235"
0赞
Astute Dlamini
12/30/2019
更多的是先生。
0赞
Leon Kunštek
12/30/2019
字符串之间的分隔符是什么?
评论
<?php
mysqli_query()
addMarks()