dsshsta97935 2015-05-01 04:43
浏览 70
已采纳

激活插件时,使用PHP在WordPress数据库中创建两个表

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?

  • 写回答

4条回答 默认 最新

  • douqiao1983 2015-05-03 10:50
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 MAC安装佳能LBP2900驱动的网盘提取码
  • ¥400 微信停车小程序谁懂的来
  • ¥15 ATAC测序到底用什么peak文件做Diffbind差异分析
  • ¥15 安装ubantu过程中第一个vfat 文件挂载失败
  • ¥20 GZ::CTF如何兼容一些靶机?
  • ¥15 etcd集群部署问题
  • ¥20 谁可以帮我一下问一下各位
  • ¥15 为何重叠加权后love图的SMD与svyCreateTableOne函数绘制基线表的不一致
  • ¥150 求 《小魔指》街机游戏机整合模拟软件
  • ¥20 你好,我想问下easyExcel下拉多选,或者复选框可以实现吗