试图创建一个插件,在wordpress数据库中提交数组数据时遇到一些问题。
$formDATA获取数组值现在问题是何时
$sql = "INSERT INTO `".$table_name ."` (`paid_arr`) VALUES \'".$formDATA."\'";
$result = $wpdb->insert($table_name,$formDATA);
收到奇怪的警告和错误,无法在wp DB中插入数据。
Warning: array_keys() expects parameter 1 to be array, string given in /home3/mahesh/public_html/project/lal/wp-includes/wp-db.php on line 1316
Warning: Invalid argument supplied for foreach() in /home3/mahesh/public_html/project/lal/wp-includes/wp-db.php on line 1318
Warning: implode() [function.implode]: Invalid arguments passed in /home3/mahesh/public_html/project/lal/wp-includes/wp-db.php on line 1327
这里是php代码;
$formDATA = array(
\'fname\' => $fname,
\'lname\' => $lname,
\'email\' => $paidemail,
\'Phone\' => $paidphone,
\'gender\' => $gender,
\'hobbies\' => $interest,
\'hear\' => $city,
\'message\' => $message_field,
\'time\' => $curtime
);
formsubmitpaid($formDATA);
function formsubmitpaid( $formDATA ) {
global $wpdb;
$formDATA = json_encode($formDATA);
$table_name = $wpdb->prefix . "padi_forms";
$sql = "INSERT INTO `".$table_name ."` (`paid_arr`) VALUES \'".$formDATA."\'";
$result = $wpdb->insert($table_name,$formDATA);
if( !$result ){
echo \'<script>alert("dfdf")</script>\';
} else {
echo \'<script>alert("No")</script>\';
}
print_r($sql);
print_r($result);
}
SO网友:Subharanjan
你不必json_encode
数组只要具有与表单数据键值相同的表结构。参考号:wpdb
$formDATA = array(
\'fname\' => $fname,
\'lname\' => $lname,
\'email\' => $paidemail,
\'Phone\' => $paidphone,
\'gender\' => $gender,
\'hobbies\' => $interest,
\'hear\' => $city,
\'message\' => $message_field,
\'time\' => $curtime
);
formsubmitpaid($formDATA);
function formsubmitpaid($formDATA) {
global $wpdb;
$table_name = $wpdb->prefix . "padi_forms";
$result = $wpdb->insert(
$table_name,
$formDATA,
array(
\'%s\',
\'%s\',
\'%s\',
\'%s\',
\'%s\',
\'%s\',
\'%s\',
\'%s\',
\'%s\'
)
);
if( $result === false )
echo \'Error\';
else
echo \'success\';
}