UPDATE
After looking at exactly how this issue was replicating, I found that I had included the form handler script in a require_once statement in my main dependency handler script. The $wpdb class would only fire off with the hard coded array of values when the script was loaded on an admin or public page of the website. Once I took the file out of my dependencies script, the $wpdb issue became consistent with or without hard coded values. I conclude then, that the issue is with this file's connection(?) to the rest of WordPress, as it's not even responding to core functions like get_header(); or get_footer();, it's acting like a standalone PHP file.
I have restarted Apache and MySQL to be sure that the server is up to date.
My main plugin file is contained in /wp-content/plugins/myplugin
The shortcode function that's rendering the form is contained in /wp-content/plugins/myplugin/shortcodes/
The script being called to handle the form is located in /wp-content/plugins/myplugin/processes/
ORIGINAL QUESTION: wpdb INSERT works with a sample array, but not with identical $_POST array
I'm creating a custom form that is for administrator use only on the front-end. The purpose of this is to record an expense into a database table in mysql. I've tried multiple methods of creating the query, using wpdb->insert and wpdb->prepare but it does not work with the $_POST superglobal.
I've created an identical hardcoded array called $sample_data, and iterating through that in the exact same way works. After using various echo and var_dump statements, I've found that the global variable $wpdb does not initiate if I use $_POST, but it works just fine if $_POST is not included in my code.
I'm new to this, so I would like to understand what I'm doing wrong and why this isn't working. Any help would be greatly appreciated.
Here is the setup I'm working with:
Ubuntu 18.04.01 localhost, WordPress 5.2.4, PHP 7.2, MySQL 5.7.27, Apache 2.4.29
The only active plugin installed on my WordPress installation is the one I'm working on, and I'm running Underscores theme with simple CSS styles applied.
Here's my code for your reference:
$table_name = $wpdb->prefix.'expenses';
if(isset($_POST['submit'])){
foreach($_POST as $post_object => $post_value){
if($post_value != NULL && $post_value !== 'submit'){
$post_object_array[] = $post_object;
$post_placeholder_array[] = '%s';
$post_value_array[] = $post_value;
}
}
$table_columns = "(".implode(', ', $post_object_array).")";
$table_placeholders = "(".implode(', ', $post_placeholder_array).")";
$query_builder = "INSERT INTO bsynfs_expenses $table_columns VALUES $table_placeholders";
$prepare_query = $wpdb->prepare($query_builder, $post_value_array);
$result = $wpdb->query($prepare_query);
}
Here is how my form is coded on the front-end. It's part of a shortcode .php file in my plugin:
<form id="add_expense" action="<?php echo($plugin_directory_url.'processes/expense-process.php'); ?>" method="post">
<input type="text" name="expense_type" value="" placeholder="expense_type">
<input type="text" name="grocery_vendor" value="" placeholder="grocery_vendor">
<input type="text" name="laundry_vendor" value="" placeholder="laundry_vendor">
<input type="text" name="product_skus" value="" placeholder="product_skus">
<input type="text" name="expense_reference" value="" placeholder="expense_reference">
<input type="text" name="expense_description" value="" placeholder="expense_description">
<input type="submit" name="submit" value="submit" id="add_expense_submit">