php – Submit form with foreign key

Question

I am using wordpress, and am trying to figure out how to add and insert a foreign key upon submission. I have a regular submit form, but am unsure how to use the foreign key.

My fields:

<form>
  <label for="First_Name">First name:</label><br>
  <input type="text" id="First_Name" name="First_Name"><br>
  
  <label for="Last_Name">Last name:</label><br>
  <input type="text" id="Last_Name" name="Last_Name"><br><br>
  
  <label for="Middle_Name">Middle Name:</label><br>
  <input type="text" id="Middle_Name" name="Middle_Name"><br><br>
  
    <label for="Home_Address">Home_Address:</label><br>
    <input type="text" id="Home_Address" name="Home_Address"><br><br>
  
    <label for="City">City:</label><br>
    <input type="text" id="City" name="City"><br><br>
  
    <label for="State">State:</label><br>
    <input type="text" id="State" name="State"><br><br>
    
    <label for="Zip">Zip</label><br>
    <input type="text" id="Zip" name="Zip"><br><br>
    
    <label for="Home_Phone">Home_Phone:</label><br>
    <input type="text" id="Home_Phone" name="Home_Phone"><br><br>
    
    <label for="Email_Address">Email_Address:</label><br>
    <input type="text" id="Email_Address" name="Email_Address"><br><br>

  <input type="submit" value="submit">

</form>

The backend:

if(isset($_POST['submit'])) {
        global $wpdb;
        
        // STUDENT INFORMATION
        $First_Name = sanitize_text_field($_POST['First_Name']);
        $Last_Name = sanitize_text_field($_POST['Last_Name']);
        $Middle_Name = sanitize_text_field($_POST['Middle_Name']);
        $Add1 = sanitize_text_field($_POST['Add1']);
        $City = sanitize_text_field($_POST['City']);
        $State = sanitize_text_field($_POST['State']);
        $Zip = sanitize_text_field($_POST['Zip']);
        $Phone = sanitize_text_field($_POST['Phone']);
        $Email = sanitize_text_field($_POST['Email']);


        //Foreign Key goes inside of Centrak Student, and references Central_Address        
        $Address_ID = $wpdb->query("FOREIGN KEY (Address_ID) REFERENCES Central_Address(ID)");
        
        
        // Central_Student Table
        $wpdb->insert('Central_Student', array('First_Name'=>$First_Name, 'Last_Name'=>$Last_Name, 'Middle_Name'=>$Middle_Name, $Address_ID));
        
        //Central_Address Table
        $wpdb->insert('Central_Address', array('Add1'=>$Add1,'City'=>$City, 'State'=>$State, 'Zip'=>$Zip, 'Email'=>$Email, 'Phone'=>$Phone));

    }

0
John Lyons 2 weeks 2022-11-16T13:52:33-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse