0

I am having a problem with my plugin that is suppose to create a table in the WordPress database when it is activated. My current code is as follows:

register_activation_hook(__FILE__, 'wp_table_install');

function wp_table_install(){
global $wpdb;
global $db_version;

$sql ="CREATE TABLE IF NOT EXISTS 'st_support_tickets'
('id' mediumint(8) unsigned NOT NULL auto_increment,
'ticket_id' varchar(36) NOT NULL,
'ticket_user_id' varchar(36) NOT NULL,
'ticket_description' TEXT default NULL,
'ticket_priority' varchar(255) default NULL,
'ticket_status' varchar(255) default NULL,
'ticket_type' varchar(255) default 'Private',
PRIMARY KEY ('id')) 
AUTO_INCREMENT=1;

CREATE TABLE IF NOT EXISTS 'st_support_priorities' 
('id' mediumint(8) unsigned NOT NULL auto_increment,
'ticket_user_id' varchar(36) NOT NULL,
PRIMARY KEY ('id'))
AUTO_INCREMENT=1;
";

require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
add_option("db_version", $db_version);
}

What is stopping these tables from being created? Is it the sql query or the syntax or what?

2
  • Please include any error messages you see. Commented May 1, 2015 at 4:47
  • Ahh yes I forgot to mention this error when the plugin is activated: "The plugin generated 619 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." Commented May 1, 2015 at 4:49

4 Answers 4

1

To solve this problem I remove all ' characters from the sql statements excluding around default values. Eg:

$sql1 ="CREATE TABLE IF NOT EXISTS  st_support_tickets 
(id  mediumint(8) unsigned NOT NULL auto_increment,
 ticket_user_id  varchar(36) NOT NULL,
 ticket_description TEXT,
 ticket_priority  varchar(255) default NULL,
 ticket_status  varchar(255) default NULL,
 ticket_type  varchar(255) default  'Private',
PRIMARY KEY ( id ))
AUTO_INCREMENT=1;
";

$sql2 = "CREATE TABLE IF NOT EXISTS st_support_priorities 
( id  mediumint(8) unsigned NOT NULL auto_increment,
 ticket_priority  varchar(36) NOT NULL,
PRIMARY KEY ( id ))
AUTO_INCREMENT=1;
";

Had no problems after this.

Sign up to request clarification or add additional context in comments.

Comments

0
register_activation_hook(__FILE__, 'wp_table_install');

function wp_table_install(){
global $wpdb;
global $db_version;

$sql ="CREATE TABLE IF NOT EXISTS 'st_support_tickets'
('id' mediumint(8) unsigned NOT NULL auto_increment,
'ticket_id' varchar(36) NOT NULL,
'ticket_user_id' varchar(36) NOT NULL,
'ticket_description' TEXT,
'ticket_priority' varchar(255) default NULL,
'ticket_status' varchar(255) default NULL,
'ticket_type' varchar(255) default 'Private',
PRIMARY KEY ('id')) 
AUTO_INCREMENT=1;

CREATE TABLE IF NOT EXISTS 'st_support_priorities' 
('id' mediumint(8) unsigned NOT NULL auto_increment,
'ticket_user_id' varchar(36) NOT NULL,
PRIMARY KEY ('id'))
AUTO_INCREMENT=1;
";

require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
add_option("db_version", $db_version);
}

3 Comments

It seems like all you did was remove the "default NULL" from the ticket_description field. This didn't do anything.
this is the error: "The plugin generated 607 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."
you have a echo somewhere in your activation hook or attached functions
0

Try with this

$sql1 ="CREATE TABLE IF NOT EXISTS 'st_support_tickets'
('id' mediumint(8) unsigned NOT NULL auto_increment,
'ticket_id' varchar(36) NOT NULL,
'ticket_user_id' varchar(36) NOT NULL,
'ticket_description' TEXT,
'ticket_priority' varchar(255) default NULL,
'ticket_status' varchar(255) default NULL,
'ticket_type' varchar(255) default 'Private',
PRIMARY KEY ('id')) 
AUTO_INCREMENT=1";

$sql2 ="CREATE TABLE IF NOT EXISTS 'st_support_priorities' 
('id' mediumint(8) unsigned NOT NULL auto_increment,
'ticket_user_id' varchar(36) NOT NULL,
PRIMARY KEY ('id'))
AUTO_INCREMENT=1;
";

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

1 Comment

I get this error: The plugin generated 318 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.
0

Tested and works fine..

register_activation_hook(__FILE__, 'wp_table_install');

function wp_table_install(){
    global $wpdb;
    global $db_version;

    $charset_collate = $wpdb->get_charset_collate();

    require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );

    $sql ="CREATE TABLE IF NOT EXISTS st_support_tickets (
          id mediumint(8) unsigned NOT NULL AUTO_INCREMENT,
          ticket_id varchar(36) DEFAULT '' NOT NULL,
          ticket_user_id varchar(36) DEFAULT '' NOT NULL,
          ticket_description TEXT DEFAULT '' NOT NULL,
          ticket_priority varchar(255) DEFAULT '' NOT NULL,
          ticket_status varchar(255) DEFAULT '' NOT NULL,
          ticket_type varchar(255) DEFAULT 'Private' NOT NULL,
          PRIMARY KEY (id)) $charset_collate;";

    dbDelta( $sql );

    $sql="CREATE TABLE IF NOT EXISTS st_support_priorities ( 
         id mediumint(8) unsigned NOT NULL AUTO_INCREMENT,
         ticket_user_id varchar(36) DEFAULT '' NOT NULL,
         PRIMARY KEY (id)) $charset_collate;";

    dbDelta($sql);

    add_option("db_version", $db_version);
}

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.