2

Trying to create a table in the wpdb when activating the plugin I am working on and getting the error "The plugin generated 2694 characters of unexpected output…"

I've been over the code and done heaps of googling to try and figure out what I've done wrong with no success. Can someone take a look and see if there is something obvious I'm not seeing? Much appreciated.

Note: The plugin still activates and creates the menu just fine, but it will not create the table in the database.

<?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 Answer 1

0

There are two things wrong.

First, you have an error in your SQL syntax. The following do not have a type defined:

    streetaddress NOT NULL,
    suburbaddress NOT NULL,
    postcodeaddress NOT NULL,

I would assume these are "text" but MySQL doesn't know that ;-) So you probably meant them to be:

    streetaddress text NOT NULL,
    suburbaddress text NOT NULL,
    postcodeaddress text NOT NULL,

The other problem is that you define $tmadm_db_version outside of the tmadm_install() function but try to use it in that function (so it is undefined inside the function). I can see that you declared it as a global variable, but you need to declare the global inside the function as well (although you're only using it inside the function, so why not just set the value inside the function instead of using a variable?).

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.
}

BTW, if you're developing plugins and need to debug the activation process, get the Debug Bar plugin and add the Debug Bar Plugin Activation plugin to it. It will make your life A LOT easier! Also, another helpful tool is something like MySQL Workbench. If you drop your SQL query in there, it will show you lines that have errors on them (which is how I found yours).

1
  • Appreciate the help. Installed the Debug Bar and Plugin Activation plugins which helped immensely, so thanks for that. I'll check out the MySQL Workbench shortly too. Commented Mar 28, 2020 at 5:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.