How to create a custom wordpress plugin

That’s not really a question, but as a not really an aswer, here are a few guidelines that you should keep in mind.

Code convention

Always try to prefix your functions with something unique (ex. your company name). This will be used like a namespace to avoid function name collision.

Database

Creating and updating a custom database table should be done in the plugin activation process, not during a custom function call.

register_activation_hook( __FILE__, 'pluginprefix_function_to_run' );

More information on the process here.

PHP

It looks like your code is defining a PHP function inside another. That’s not how you should do it. Functions should all be declared at the same level (or better, in a class).

WordPress

WP let you store almost everything you want in the wp_options table. If you need to add a custom config (like your admin field), you should try to store it in this table.
You can easily set and get options with

$value = get_option( 'my_custom_option_name' );
update_option( 'my_custom_option_name', 'new value here')

Link to the docs get_option and update_option

Next step for you might be to try to add this value somewhere in a page with a short code or in your theme.