WordPress,尝试插入数据时出错>>注意:wpdb::Prepare调用不正确

时间:2018-02-09 作者:Julian

我有以下代码将多行插入到我的自定义WordPress表中。

当我运行表单提交数据并将其插入WordPress数据库时,我收到以下错误消息:

注意:wpdb::prepare的调用不正确。不支持的值类型(数组)。警告:mysqli\\u real\\u escape\\u string()要求参数2为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));
我在$\\u帖子中传递给代码的数组如下所示:

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 )  
) 
我希望有人能帮助我,因为我一直在努力解决这个问题,直到今天。

1 个回复
SO网友:Julian

我已经解决了这个问题,问题在于我创建阵列的方式。我使用的是多维数组,需要在帖子中调用数组的正确级别。

通过调用

$\\u POST[“客户”]

这是我可以访问正确行的数组的名称。以下是阵列的外观。很痛苦,但我最终还是解决了,所以我想确保我能为其他人分享。

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 Name Change

我读到,使用wp\\uu之外的其他东西作为数据库前缀更安全,所以我下载了我的数据库前缀wp\\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu