使用foreach制作表格,尝试使用每个表格行提交数据

时间:2019-06-13 22:16:35

标签: php mysql wordpress

自从编写任何代码以来已经有10多年了,所以我可能离这里很远。我有一张表,显示教室中的所有学生,都由一个foreach循环填充,并带有一个添加框来输入他们的成绩。我需要1个提交按钮,以便在我的数据库的成绩表中为每个学生插入一行新记录,上面有他们的成绩和作业名称。

我知道我需要确定每个成绩值,只是不确定如何,并且只会在最后一行发布该人。

global $wpdb;
$asnames = $wpdb->get_results( "SELECT * FROM assignments" );
$results = $wpdb->get_results( "SELECT * FROM enrolled" );

    ?>
    <div class="container"><center>
      <form action ="<?php echo $_SERVER['REQUEST_URI']; ?>" method ="post">
      <table class="form-table" id='topper'>
      <tbody>
      <tr>
      <th>Assignment Name</th>
      </tr>
      <tr>
      <td>
        <select name="Assignment">
      <?php foreach($asnames as $asname) { ?>
      <option value="<?php echo $asname->id ?>"><?php echo $asname->assign_name ?></option>
  <?php

    } ?>
        </select>
      </td>

      </tr>
      </tbody>
    </table></center>
    </div>

    <br><br>

    <div class="container"><center>
    <form action ="<?php echo $_SERVER['REQUEST_URI']; ?>" method ="post">
    <table class="form-table" id='student'>
    <tbody>
    <tr>
    <th>Students Name</th>
    <th>Points</th>
    </tr>
    <?php foreach($results as $row){ ?>
    <tr>
    <td><?php echo "$row->student_name" ?></td>
    <td><input type="text" class="regular-text" name="Grade"></td>
    </tr>
    <?php } ?>
    <!-- </tbody> -->
    </table>
    <p><input type="submit" name="submitinsert" id="submitinsert" class="button button-primary" value="Submit"/></p>            </form>
  </center></div>
<?php


  if ($_POST['submitinsert']){
    $result = $wpdb->insert(
             'grades',
             array(
             "student_name" => $row->student_name,
             "assignment_id" => $asname->id ,
             "grade" => $_POST['Grade']
             )
         );
     }

我似乎无法抓住它来插入要插入的表中的每一行,只插入了最后一行。

编辑:我现在想出了成绩输入,只是试图使用单个提交按钮将每个循环放入数据库中。

0 个答案:

没有答案