无法在wordpress中创建表

pdkcd3nj  于 2022-11-02  发布在  WordPress
关注(0)|答案(2)|浏览(131)

我不能在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()显示:
数组(1){ [“wp_submit”]=〉字符串(23)“创建的表wp_submit”}

jgzswidk

jgzswidk1#

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 );

 }

请尝试以下代码。

  • 谢谢-谢谢
px9o7tmv

px9o7tmv2#

您可以使用以下代码:

global $jal_db_version;
$jal_db_version = '1.0';

function jal_install() {
    global $wpdb;
    global $jal_db_version;

    $table_name = $wpdb->prefix . 'liveshoutbox';

    $charset_collate = $wpdb->get_charset_collate();

    $sql = "CREATE TABLE $table_name (
        id mediumint(9) NOT NULL AUTO_INCREMENT,
        time datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
        name tinytext NOT NULL,
        text text NOT NULL,
        url varchar(55) DEFAULT '' NOT NULL,
        PRIMARY KEY  (id)
    ) $charset_collate;";

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

    add_option( 'jal_db_version', $jal_db_version );
}
register_activation_hook( __FILE__, 'jal_install' );

相关问题