0
votes

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">

1
Are these codes inside a themes, plugin or external in a file outside the standard wp framework? Explain exactly where each of these codes is. Please ;) - Kelvin Mariano
@KelvinMariano I've updated the question to reflect your comment. - syedmh
Not sure, please confirm me. in the expense-process.php file are you not loading wordpress just passing normal correct codes? require the wp-load.php file which is at the root of the wordpress installation that wordpress will be loaded so it will probably work - Kelvin Mariano
please if it still doesn't work, could i put this plugin online just so i can test locally and see what the real problem is? There are several factors that may be causing this. updated my answer with an example - Kelvin Mariano
@KelvinMariano I'll do test that out. Before that, though, I haven't had to do that at all for any of my plugin files until now, and they all run wordpress functions just fine. Is there a reason why this isn't working out? If this doesn't work, I'll upload the plugin online so you could have a look at it. Thank you so much! - syedmh

1 Answers

0
votes

Try this, for me it worked and worked here. 1 - Create a file in your template and add this code, then create a page in admin and select the template "Test" or whatever you want to call it.

Then just test

<?php 
/*
    Template Name: Teste
*/
global $wpdb;
$table_name = $wpdb->prefix.'expenses';

if(isset($_POST['submit'])){
    echo "<pre>";
    print_r($_POST);
    echo "</pre>";
    exit();

    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);
}

get_header();
?>
 <form id="add_expense" action="<?php echo get_permalink(); ?>" 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">
</form>
<?php 
get_footer();

or in file /wp-content/plugins/myplugin/processes/:

<?php
require_once '../../../wp-load.php';


global $wpdb;
$table_name = $wpdb->prefix.'expenses';

if(isset($_POST['submit'])){
echo "<pre>";
print_r($_POST);
echo "</pre>";
exit();

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);

}