0

So its been over 10 years since writing any code what so ever so i might be way off here. I have a table showing all students in a classroom populated by a foreach loop with an added box to input their grade. I need 1 submit button to insert one new row per student in the grades table in my database with their grade and the assignment name that is selected above.

I know i need to identify each grade value im just unsure how and it's only posting the person in the last row.

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']
             )
         );
     }

I cant seem to get it to grab each row as it is in the table to insert, only the last row gets put in.

EDIT: I got the grade input worked out now just trying to get each loop put into the database with the single submit button.

  • all your inputs have the same name "grade" so thats why you only get the last one every time –  Jun 13 '19 at 22:31
  • I understand that and im unsure how to make it match the table row but im only getting 1 entry into the database at the moment anyways. thats the big problem. – C.j. Burleigh Jun 13 '19 at 22:35
  • 1
    Pass the student id via `` for each row or use `name="Grade[= $row->id; ?>]"`. You can also create the `
    ` tag inside the loop, one form per student, and one submit `button` per row, depending on your row count.
    – msg Jun 13 '19 at 22:38
  • I will have up to 25 rows at times i would much rather 1 button. I will try the name="Grade[= $row->id; ?>]" method but i cant really test it until i can pass more than 1 line into the database. – C.j. Burleigh Jun 14 '19 at 05:11
  • That will make each form field unique, avoiding your problem of having only one value. Additionally you will get the grades indexed by `student_id` in the `$_POST['Grade']` array and you won't need the `hidden` input either. – msg Jun 14 '19 at 08:11
  • Ok i just need to figure out how to have the button put each loop into the database. got the grade input worked out now. – C.j. Burleigh Jun 15 '19 at 15:39
  • You **need** to submit an array, review previous comments and [this question](https://stackoverflow.com/questions/27314062) – msg Jun 17 '19 at 17:10

0 Answers0