Wordpress, Error when trying to insert data >> Notice: wpdb:was called incorrectly

I have the following code to insert multiple rows into my custom WordPress table.

When I run the form to submit the data and insert it to the WordPress database I get the this error message:

Notice: wpdb::prepare was called incorrectly. Unsupported value type (array). and Warning: mysqli_real_escape_string() expects parameter 2 to be string,

    global $wpdb;
    $values = array();
    $place_holders = array();

    $query = "INSERT INTO wp_1com_dc_attedance (customer_id, location_id, funding_source, attend_date, approval_date, attend_am, attend_pm) VALUES";

    foreach($_POST as $key => $value)
    {
         array_push($values, $value);
         $place_holders[] = "('%s', '%s', '%s', '%s', '%s', '%s', '%s')";
    }

    $query .= implode(', ', $place_holders);
    $wpdb->query( $wpdb->prepare("$query", $values));

The array I am passing to the code in the $_POST is as follows:

Array ( 
[customer_id] => Array ( [0] => 2 [1] => 4 ) 
[locations_id] => Array ( [0] => 1 [1] => 1 ) 
[funding_source] => Array ( [0] => 1 [1] => 2 ) 
[attend_date] => Array ( [0] => 2018-02-09 [1] => 2018-02-09 ) 
[approval_date] => Array ( [0] => 2018-02-08 [1] => 2018-02-08 ) 
[attend_am] => Array ( [0] => 1 ) 
[attend_pm] => Array ( [0] => 1 )  
) 

I hope somebody can help me as I have been working to try and fix this for the best part of today.

Solutions

I have fixed this, the issue was in the way I was creating the array. I was using a multi-dimensional and needed to call the correct level of the array in the post.

By calling

$_POST['customer']

which was the name of the array I could access the correct rows. Here is what the array looks like. Quite painful but I solved it in the end so wanted to ensure I shared for others.

Array ( 
[customer] => Array ( 
    [0] => Array ( [customer_id] => 6 [locations_id] => 1 [funding_source] => 2 [attend_date] => 2018-02-12 [approval_date] => 2018-02-12 ) 
    [1] => Array ( [customer_id] => 2 [locations_id] => 1 [funding_source] => 1 [attend_date] => 2018-02-12 [approval_date] => 2018-02-12 ) 
) 
[approve_data] => Approve Attendance )

Similar questions

Notice: map_meta_cap was called incorrectly
I created an external script to import json data into a custom post type that uses wp-load.php. Everything works fine but after every update I get the message Im calling the script inside a child theme and post_type_exists returns false but works fine... Ive seen a similar notice in a core track thread but it talks about comments being the issue an...
PHP Notice: wp_enqueue_script was called incorrectly. Scripts and styles should not be registered
Can someone help me fix this error? PHP Notice: wp_enqueue_script was called incorrectly. Scripts and styles should not be registered or enqueued until the wp_enqueue_scripts, admin_enqueue_scripts, or login_enqueue_scripts hooks. Please see Debugging in WordPress for more information. (This message was added in version 3.3.0.) in /nas/content/live...
Notice: register_uninstall_hook was called incorrectly
I automatically updated my wordpress installation to the latest version. Everything looked normal unit "updating database..." where it got stuck. I waited for a long while and then closed the page, since nothing was happening. Then I got a error message saying something about maintenance, which I got rid of after deleting the maintenance file. Word...
Notice: wp_enqueue_script was called incorrectly
So I'm busy in Wordpress. I have a theme for the site and all. It's all working fine. Then I want to activate the new theme. BENG BENG, white screen of death. I debugged and this is the error: Line 2944 is just the line that throws an error. I already checked that. Anyone ever experienced and solved this? EDIT: Referencing lines:

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy .