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?
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);
}
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);
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.
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);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.