dbDelta not CREATING TABLE

时间:2019-12-28 作者:Arvin Bahilango

这是我的密码。请帮忙

function lapizzeria_database() {
   global $wpdb;

   global $lapizzeria_db_version;
   $lapizzeria_db_version = "1.0";

   $table = $wpdb->prefix . \'reservation\';

   $charset_collate = $wpdb->get_charset_collate();

   //SQL Statement

   $sql = "CREATE TABLE $table (
        id mediumint(9) NOT NULL AUTO_INCREMENT,
        name varchar(50) NOT NULL,
        date datetime NOT NULL,
        email varchar(50) DEFAULT \'\' NOT NULL,
        phone varchar(10) NOT NULL
        message longtext NOT NULL,
        PRIMARY KEY (id)
   ) $charset_collate; ";

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

add_action(\'after_setup_theme\', \'lapizzeria_database\');

1 个回复
最合适的回答,由SO网友:Chetan Vaghela 整理而成

缺少逗号结尾phone varchar(10) NOT NULL 第行,在将创建的表之后添加逗号。我已经测试过了https://prnt.sc/qgqmog

function lapizzeria_database() {
   global $wpdb;

   global $lapizzeria_db_version;
   $lapizzeria_db_version = "1.0";

   $table = $wpdb->prefix . \'reservation\';

   $charset_collate = $wpdb->get_charset_collate();

   //SQL Statement

   $sql = "CREATE TABLE $table (
        id mediumint(9) NOT NULL AUTO_INCREMENT,
        name varchar(50) NOT NULL,
        date datetime NOT NULL,
        email varchar(50) DEFAULT \'\' NOT NULL,
        phone varchar(10) NOT NULL,
        message longtext NOT NULL,
        PRIMARY KEY (id)
   ) $charset_collate; ";

   require_once(ABSPATH . \'wp-admin/includes/upgrade.php\');
   dbDelta($sql);
}
add_action(\'after_setup_theme\', \'lapizzeria_database\');