'Is there any ways to connect the external database with wordpress elementor and store form data in different pages to different table?

I tried this and was only able to connect to first table for homepage form and was not able to do the same thing in form form different page:\

    add_action( 'elementor_pro/forms/new_record' , function( $record, $ajax_handler ) { 
        $form_name = $record->get_form_settings( 'form_name' );
        if ( 'FORM_ONE' == $form_name )
        {
        $raw_fields = $record->get( 'fields' );
        $fields = [];
        foreach ( $raw_fields as $id => $field ) {
        $fields[ $id ] = $field['value'];
        }
        $wpdb_c = new wpdb( "DB-name", "DB_Pass", "database-name", "localhost"  );
        $output['success'] = $wpdb_c->insert('Tablename', array( 
            'name' => $fields['name'],
            'email' => $fields['email'],
            'message' => $fields['message']));
    
        $ajax_handler->add_response_data( true, $output );
        }
        elseif ( 'FORM_TWO' == $form_name ) {
        $raw_fields = $record->get( 'fields' );
        $fields = [];
        foreach ( $raw_fields as $id => $field ) {
        $fields[ $id ] = $field['value'];
        }
    
        $wpdb_d = new wpdb( "DB-name", "DB_Pass", "database-name", "localhost"  );
        $output['success'] = $wpdb_d->insert('Tablename', array(
            'first_name' => $fields['first_name'], 
            'last_name'  => $fields['last_name'],
            'email1' => $fields['email1'],
            'email2' => $fields['email2'],
            'dob' =>  $fields['dob'],
            'phone_no' => $fields['phone_no'],
            'country' => $fields['country'],
            'gender' =>  $fields['gender'],
            'preference' => $fields['preference'],
            'details' => $fields['details'],
            'where_did_hear' => $fields['where_did_hear'],
            'file' => $fields['file']));
            $ajax_handler->add_response_data( true, $output);
        }
    
        }, 10, 2);

I am using backend database as MYSQL



Solution 1:[1]

Have you considered using REST API? Connecting external database via rest api such as Supabase might be solution you are looking for.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Kamaldeep Singh