我正在为woocommerce创建一个wordpress插件,我创建的表格没有插入到数据库中?我收到错误The plugin generated 5090 characters of unexpected output during activation. If you notice “headers already sent” messages, problems with syndication feeds or other issues, try deactivating or removing this plugin.
以下是代码:
register_activation_hook( __FILE__, 'pato_install' );
register_activation_hook( __FILE__, 'pato_install_data' );
function pato_install() {
global $wpdb;
$table_name = $wpdb->prefix . 'pato_shipping';
$sql = "CREATE TABLE $table_name (
id mediumint(9) NOT NULL AUTO_INCREMENT,
pato_shipping_data text NOT NULL,
);";
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
dbDelta( $sql );
}
function pato_install_data() {
global $wpdb;
$table_name = $wpdb->prefix . 'pato_shipping';
//shipping options flat rate charges
$option_array = array(
'plants_fast' => '45.5',
'plants_standard' => '0');
//shipping condition values
$condition_array = array(
'plant_quantity_m1_1' => '3',
'plant_quantity_m1_2' => '6',
'plant_quantity_m2_1' => '6',
'plant_quantity_m3_1' => '3');
//shipping item charges
$item_charge_array = array(
'plants_ml_1_charge' => '49.5',
'plants_m2_1_charge' => '69.5',
'plants_m3_1_charge' => '89.5');
$data_array = array_merge($option_array,$condition_array,$item_charge_array);
$data_array = serialize($data_array);
$wpdb->insert(
$table_name,
array('pato_shipping_data' => $data_array)
);
}
我哪里出错了?
答案 0 :(得分:1)
您的create table查询中有一些错误。您的自动增量列需要是一个键,并且在第二列之后有一个额外的尾随逗号。尝试:
CREATE TABLE $table (
id mediumint(9) NOT NULL PRIMARY KEY AUTO_INCREMENT,
pato_shipping_data text NOT NULL
)
在将它们合并到插件中之前,我通常会尝试直接运行查询。它使调试变得更容易。