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

标签 php mysql

我需要同时输入类(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/

相关文章:

php - 使用 grep 在目录中的所有文件中搜索字符串的实例,使用 PHP bash 脚本

php - 从两个单独的表中选择数据作为 MySQL/PHP 中的新闻提要

php - 连接mysql函数的区别?

mysql - 在 From 子句中创建子查询以引用存档表

mysql - 使用Mysql显示正确的cumulative_sum值

javascript - 如何将 POST 变量添加并保存到 Qualtrics?

php - mysql 连接多个where

php - 开发大型连接/断开连接应用程序的最佳方法是什么

mysql - 将 mysql 从 windows 迁移到 unix。生成文件的语法错误

mysql - 在 HQL 中使用 MySQL 将字符串转换为时间