首页 > 解决方案 > 无法在 wordpress 中创建表格

问题描述

我无法在 wordpress 中创建表格,我不明白为什么?

private function create($prefix)
{
    $sql = "CREATE TABLE " . $prefix . "submit (
            submit_id bigint PRIMARY KEY AUTO_INCREMENT,
            post_id bigint,
            user_id bigint,
            author text,
            user_email text,
            source text,
            pass text,
            language text,
            time datetime,
            CONSTRAINT post_id FOREIGN KEY (post_id) REFERENCES wp_posts(ID),
            CONSTRAINT user_id FOREIGN KEY (user_id) REFERENCES wp_users(ID)
        )";

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

节目var_dump()

array(1) { ["wp_submit"]=> string(23) "创建表 wp_submit" }

标签: phpwordpress

解决方案


add_action("after_switch_theme", "my_custom_table_setup");

 function my_custom_table_setup() 

 {



global $wpdb;
    $create_table_query = "


CREATE TABLE {$wpdb->prefix}products_by_sku (
          id int(11) NOT NULL  auto_increment,
          product_id int(11) NOT NULL,
          lot_id varchar(25) NOT NULL,
          title varchar(255) NOT NULL,
          LP varchar(25) NOT NULL,
          catgeory varchar(255) NOT NULL,
          product_name varchar(255) NOT NULL,
          UPC varchar(255) DEFAULT NULL,
          ASIN varchar(255) DEFAULT NULL,
          original_retail_price float DEFAULT NULL,
          quantity int(11) DEFAULT NULL,
          total_original_retail float NOT NULL DEFAULT 0,
          stock_image text,
          PRIMARY KEY  (id)
        ) ENGINE=InnoDB DEFAULT CHARSET=latin1;   


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



 }

试试这个代码。

谢谢你。


推荐阅读