无法在现有wordpress数据库中创建表

时间:2016-10-20 12:12:49

标签: php wordpress

我想在现有的wordpress数据库中创建一个表,但我无法创建它。我在自定义插件文件中编写了这段代码。请帮我完成这段代码。

function create_xml_table(){
    global $wpdb;
    $tableName = $wpdb->prefix."XMLdata";
    $charset_collate = $wpdb->get_charset_collate();
    $sql = "CREATE TABLE $tableName(id int(10) NOT NULL AUTO_INCREMENT,
                user_id varchar(255) NOT NULL,
                post_id varchar(255) NOT NULL,
                value varchar(255) DEFAULT '' NOT NULL,
                time timestamp ON UPDATE CURRENT_TIMESTAMP,
                PRIMARY KEY  (id)
            ) $charset_collate;";
    require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
    dbDelta( $sql );
    register_activation_hook(__FILE__, 'create_xml_table');
}

1 个答案:

答案 0 :(得分:0)

You have to do like this.

function create_xml_table(){

global $wpdb;
$tableName = $wpdb->prefix."XMLdata";
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE $tableName(id int(10) NOT NULL AUTO_INCREMENT,
                            user_id varchar(255) NOT NULL,
                            post_id varchar(255) NOT NULL,
                            value varchar(255) DEFAULT '' NOT NULL,
                            time timestamp ON UPDATE CURRENT_TIMESTAMP,
                            PRIMARY KEY  (id)
                            )$charset_collate;";
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
dbDelta( $sql );
}

register_activation_hook(__FILE__, 'create_xml_table');