“插件生成了2,694个字符的意外输出…”插件激活时,CREATE TABLE SQL命令不起作用

时间:2020-03-27 作者:Daniel Florian

当激活我正在使用的插件时,试图在wpdb中创建一个表,并出现错误“插件生成了2694个字符的意外输出…”

我一直在浏览代码,并在google上搜索了大量内容,试图找出我做错了什么,但没有成功。有人能看看我是否有什么明显的东西没看到吗?非常感谢。

Note: 插件仍然可以激活并创建菜单,但它不会在数据库中创建表。

<?php

// plugin details in comment at top


defined(\'ABSPATH\') or die(\'You should not be here…\');


function tmadm_activation() {
    flush_rewrite_rules();
}
register_activation_hook( __FILE__, \'tmadm_activation\' );


function tmadm_deactivation() {
    flush_rewrite_rules();
}
register_deactivation_hook( __FILE__, \'tmadm_deactivation\' );

// Insert "Students" Table to WP Database

global $tmadm_db_version;
$tmadm_db_version = \'0.1.0\';

function tmadm_install() {
    global $wpdb;
    global $jal_db_version;

    $table_name = $wpdb->prefix . \'tmadm_students\';

    $charset_collate = $wpdb->get_charset_collate();

    $sql = "CREATE TABLE $table_name (
        id int NOT NULL AUTO_INCREMENT,
        studentnumber text NOT NULL,
        dojang text NOT NULL,
        firstname tinytext NOT NULL,
        lastname tinytext NOT NULL,
        title text NOT NULL,
        gender text NOT NULL,
        bbtitle text,
        titleid text,
        instructorcertexpire datetime DEFAULT \'0000-00-00 00:00:00\',
        wwcnumber text,
        wwcexpire date DEFAULT \'0000-00-00\',
        firstaidnumber text,
        firstaidrto text,
        firstaidexpire date DEFAULT \'0000-00-00\',
        cprexpire date DEFAULT \'0000-00-00\',
        streetaddress NOT NULL,
        suburbaddress NOT NULL,
        postcodeaddress NOT NULL,
        phhome text,
        phwork text,
        phmobile text,
        email text NOT NULL,
        dateofbirth date NOT NULL,
        height text NOT NULL,
        weight text NOT NULL,
        lastupdated date NOT NULL,
        beltsize text NOT NULL,
        uniformsize text NOT NULL,
        occupation text NOT NULL,
        nationality text,
        emergencycontact text NOT NULL,
        relationship text NOT NULL,
        phhomeemergency text NOT NULL,
        phworkemergency text NOT NULL,
        phmobileemergency text NOT NULL,
        reference text,
        referenceinternet text,
        applicationdate date DEFAULT \'0000-00-00\' NOT NULL,
        currentrank text NOT NULL,
        rankid text NOT NULL,
        ranktitle text NOT NULL,
        consentpart1 text NOT NULL,
        consentpart2 text NOT NULL,
        consentpart3 text NOT NULL,
        consentpart4 text NOT NULL,
        consentpart5 text NOT NULL,
        consentpart6 text NOT NULL,
        consentpart7 text NOT NULL,
        consentpart8 text NOT NULL,
        consentpart9 text NOT NULL,
        consentpart10 text NOT NULL,
        consentpart11 text NOT NULL,
        consentpart12 text NOT NULL,
        completeconsent text NOT NULL,
        Consentby text NOT NULL,
        active text NOT NULL,
        agreedfee text,
        autobilled text,
        billfrequency text,
        newfee text,
        PRIMARY KEY  (id)
    ) $charset_collate;";

    require_once( ABSPATH . \'wp-admin/includes/upgrade.php\' );
    dbDelta( $sql );

    add_option( \'tmadm_db_version\', $tmadm_db_version );
}

// Insert initial data into Students Table
function tmadm_install_data() {
    global $wpdb;

    $table_name = $wpdb->prefix . \'tmadm_students\';

    $wpdb->insert( 
        $table_name, 
        array( 
            \'studentnumber\' => \'00XX1\',
    //rest of this data removed as it contains personal data
        )
    );
}
register_activation_hook(__FILE__, \'tmadm_install\');
register_activation_hook(__FILE__, \'tmadm_install_data\');

// Plugin Settings Menu
add_action( \'admin_menu\', \'tmadm_admin_menu\' );

function tmadm_admin_menu() {
    add_menu_page( \'TMA Dojang Manager\', \'Dojang Manager\', \'manage_options\', \'tmadm_settings\', \'tmadm_settings_page\', \'\', 50  );
    add_submenu_page( \'tmadm_settings\', \'General Settings\', \'General Settings\', \'manage_options\', \'tmadm_settings\', \'tmadm_settings_page\' );
    add_submenu_page( \'tmadm_settings\', \'Dojang Settings\', \'Dojang Management\', \'manage_options\', \'tmadm_settings\', \'tmadm_settings_page\' );
    add_submenu_page( \'tmadm_settings\', \'Instructor Settings\', \'Instructor Management\', \'manage_options\', \'tmadm_settings\', \'tmadm_settings_page\' );
    add_submenu_page( \'tmadm_settings\', \'Student Settings\', \'Student Management\', \'manage_options\', \'tmadm_settings\', \'tmadm_settings_page\' );
}

function tmadm_settings_page(){
    include \'admin/tmadm-admin-page.php\';
}

?>

1 个回复
最合适的回答,由SO网友:butlerblog 整理而成

有两件事不对。

首先,您的SQL语法有一个错误。以下未定义类型:

    streetaddress NOT NULL,
    suburbaddress NOT NULL,
    postcodeaddress NOT NULL,
我假设这些是“文本”,但MySQL不知道;-)所以你可能想让他们:

    streetaddress text NOT NULL,
    suburbaddress text NOT NULL,
    postcodeaddress text NOT NULL,
另一个问题是您定义$tmadm_db_versiontmadm_install() 函数,但尝试在该函数中使用它(因此它在函数中未定义)。我可以看到您将其声明为全局变量,但您需要声明全局变量inside 函数也是如此(虽然您只在函数内部使用它,那么为什么不在函数内部设置值而不是使用变量呢?)。

global $tmadm_db_version;
$tmadm_db_version = \'0.1.0\';

function tmadm_install() {
    global $wpdb;
    global $jal_db_version; // No global $tmadm_db_version inside the function?

    // ... other stuff ...

    add_option( \'tmadm_db_version\', $tmadm_db_version ); // Using an undefined varibable here.
}
顺便说一句,如果您正在开发插件并需要调试激活过程,请获取Debug Bar plugin 并添加Debug Bar Plugin Activation 插件到它。这会让你的生活变得更轻松!另外,另一个有用的工具是MySQL Workbench. 如果您将SQL查询放在其中,它将显示有错误的行(我就是这样找到您的)。