admin管理员组文章数量:1193314
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.
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.
Share Improve this question asked Feb 9, 2018 at 16:37 JulianJulian 114 bronze badges 1 |1 Answer
Reset to default 1I 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 )
本文标签:
版权声明:本文标题:database - Wordpress, Error when trying to insert data >> Notice: wpdb::prepare was called incorrectly 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738481585a2089172.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
$query
(specifically in the second line of yourforeach
loop). Each element of the$place_holders
array is what you should append to$query
, but you are appending it as many times as there are keys in your$_POST
array. This comment section is not long enough to elaborate and dump an example, but if you try to echo your$query
I think the issue sould become quite clear :) – Iceable Commented Feb 9, 2018 at 18:02