gpt4 book ai didi

php - 将 while 循环中文本框中的数据提交到数据库

转载 作者:行者123 更新时间:2023-11-29 22:18:44 24 4
gpt4 key购买 nike

我需要同时输入类(class)中所有学生提供的所有科目的分数。因此,我检索了学生 ID,检索了学生提供的科目列表,并在每个科目下放置了一个文本框。

现在,我想提交数据库中的分数以及科目名称和学号,但是分数没有存储,只存储科目名称和学号。

这是我的代码:

<form method="post">
<?php
include "includes/dbcon.php";
$subject_name ="";
echo "<table border='1'><thead><tr><td>Students Name</td>";
$query_subjects = mysqli_query($link,"SELECT * FROM junior_subjects ORDER BY subject_name ASC");
while ($row_subject=mysqli_fetch_array($query_subjects))
{
$subject_name .= $row_subject['subject_name'];
echo "<td>".$row_subject['subject_name']."</td>";
}
echo "</tr></thead>";
$query_students = mysqli_query($link,"SELECT * FROM students WHERE class_cat='Junior'");
while ($row_students=mysqli_fetch_array($query_students))
{
$student_id = $row_students['student_id'];
echo "<tr><td>".$row_students['student_id']."</td>";
$query_subjects2 = mysqli_query($link,"SELECT * FROM junior_subjects ORDER BY subject_name ASC");
while ($row_subject2 =mysqli_fetch_array($query_subjects2))
{
$subject_name2 =$row_subject2['subject_name'];
echo "<td>
<input type='text' hidden name='$subject_name2'>
<input type='text' size='4' name='$subject_name2'>
</td>";
/////
if (isset($_POST['submit']))
{
$score = $_POST[$subject_name2];
mysqli_query($link,"INSERT INTO score_sheet(student_id,subject_name,score) VALUES('$student_id','$subject_name2','$score') ");
}
}
}
?>
<input type='submit' name='submit'>
</form>

最佳答案

您应该将输入 score 作为数组,并进行 foreach 循环来插入查询。

你应该有这样的输入字段

并执行这样的 foreach 循环

    $score = $_POST['score'];
foreach ($score as $key)
{
$query = "INSERT INTO score_sheet(student_id,subject_name,score) VALUES('$student_id','$subject_name2','$key') ";
$query = mysqli_query($link,$query);
}

注意:

里面不应该有两个表单。

我做了一个eval为你

警告:

进行如此批量的输入根本不是一个好的做法。

关于php - 将 while 循环中文本框中的数据提交到数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30951966/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com