Errors when trying to use a plugin to write to the database

You should never have to read wp-config to get database credentials. WordPress has built-in functions which are generally more secure for this sort of operation.

In your plugin, simply call
global $wpdb;

and you will be connected to the WP database. You can then perform whatever queries you need – select, update, add, etc. For example:

$results = $wpdb->get_results( 'SELECT * FROM wp_options WHERE option_id = 1', OBJECT );

Please see https://codex.wordpress.org/Class_Reference/wpdb for details. This should be both safer and much easier than reading in credentials and manually connecting.