C.j. Burleigh
C.j. Burleigh

Reputation: 11

Posting a simple form to 2 tables with PHP

I must be missing thing here, nothing is being sent to the database. Im simply trying to send this information to 2 tables. Updating one and inserting a new row into another.

I have tested the data and its being gathered, and have tested sending to only 1 table and still not sending any information.

Form:

  <div class="container"><center>
    <form action ="<?php echo $_SERVER['REQUEST_URI']; ?>" method ="post">
    <table class="form-table" id='student'>
    <tbody>
    <tr>
    <th>Class</th>
    <th>Assignment Id</th>
    <th>Student Name</th>
    <th>Grade</th>
    </tr>
    <?php foreach($students as $row){ ?>
    <tr>
    <input type="hidden" name="student_name" size="30" value="<?php echo "$row->student_name" ?>">
    <input type="hidden" name="student_id" size="30" value="<?php echo "$row->student_id" ?>">
    <td><Center><?php echo "$class"; ?></Center></td>
    <td><Center><?php echo "$assign"; ?></Center></td>
    <td><Center><?php echo "$row->student_name"; ?></Center></td>
    <td><Center><input type="text" name="grade" size="15" placeholder="Points"></Center></td>
    </tr>
    <?php } ?>
  </tbody>
    </table>
    <p class="submit"><Center><input type="submit" name="submitgrade" id="submitgrade" class="button button-primary" value="Submit" /></center></p>  </form>
  </center></div>

MYSQL query:

  if ($_POST['submitgrade']){
    global $wpdb;
    $success = $wpdb->insert(
             'sodae_grades',
             array(
             "student_name" => $_POST['student_name'],
         "student_id" => $_POST['student_id'],
         "class_of" => $class,
             "assignment_name" => $assign,
             "grade" => $_POST['grade']
       )
     );

       $success2 = $wpdb->update(
           'sodae_enrolled',
           array(
           "grade" => $_POST['grade']
          ),
          array(
            "student_id" => $_POST['grade'] + $count
          )
        );

        if($success) {
          echo ' Inserted successfully';
                    } else {
             echo 'not';
             }
         if($success2) {
              echo ' Inserted successfully2';
                        } else {
                 echo 'not2';
                 }
        }

Both statements are coming back as not posted and nothing is in the database.

EDIT: I have tried define('WP-DEBUG', true); and $wpdb->print_error(); with no errors I have checked other forms and none of them are posting other than when i used the wp_insert_user but that's only for user entry of course.

Upvotes: 1

Views: 103

Answers (2)

C.j. Burleigh
C.j. Burleigh

Reputation: 11

So by using $wpdb->last_error i was able to fix a few errors linking to fields in the database and fix some typos. easy fix once I had the error guide.

Upvotes: 0

DubVader
DubVader

Reputation: 1072

Perhaps it's not recognizing the table prefix. You should also specify the type of data, like string (%s) or integer (%d). Try something like this:

    global $wpdb;
    $table_name = $wpdb->prefix . 'sodae_enrolled';
    $wpdb->insert($table_name, array(
         "student_name" => $_POST['student_name'],
         "student_id" => $_POST['student_id'],
         "class_of" => $class,
         "assignment_name" => $assign,
         "grade" => $_POST['grade']
    ), array(
        "%s",
        "%s",
        "%s",
        "%s",
        "%s"
    ));

Upvotes: 1

Related Questions