激活挂钩未创建表

时间:2016-10-07 作者:Milos

出于某种原因,我的激活挂钩没有创建新的数据库表。

代码:

function mm_booking_activate() {
    global $wpdb;
    $mm_booking_table_name = $wpdb->prefix . "mm_booking";
    $mm_booking_db_version = \'1.0\';
    $charset_collate = $wpdb->get_charset_collate();

    if ( $wpdb->get_var( \'SHOW TABLES LIKE \' . $mm_booking_table_name ) != $mm_booking_table_name ) {
        $sql = "CREATE TABLE $mm_booking_table_name (
            id mediumint(9) NOT NULL AUTO_INCREMENT,
            first_name varchar(255),
            last_name varchar(255),
            address varchar(255),
            lat decimal(10, 8) NOT NULL,
            lng decimal(11, 8) NOT NULL,
            field enum(\'option1\', \'option2\', \'option3\'),
            phone_number varchar(255),
            email varchar(255),
            date_time_from datetime DEFAULT CURRENT_TIMESTAMP,
            date_time_to datetime DEFAULT CURRENT_TIMESTAMP,
      status enum(\'pending\', \'approved\', \'rejected\') DEFAULT \'pending\',
      modification_time datetime ON UPDATE CURRENT_TIMESTAMP,
      creation_time datetime DEFAULT CURRENT_TIMESTAMP,
      PRIMARY KEY  (id)
    ) $charset_collate;";

        require_once( ABSPATH . \'wp-admin/includes/upgrade.php\' );
        dbDelta( $sql );
        add_option( \'mm_booking_db_version\', $mm_booking_db_version );
    }
}
register_activation_hook( __FILE__, \'mm_booking_activate\' );

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

表名需要在检查其是否存在的行上加引号。我对它进行了测试,修复后,成功创建了表。以下是完整代码:

function mm_booking_activate() {
    global $wpdb;
    $mm_booking_table_name = $wpdb->prefix . "mm_booking";
    $mm_booking_db_version = \'1.0\';
    $charset_collate = $wpdb->get_charset_collate();

    if ( $wpdb->get_var( "SHOW TABLES LIKE \'{$mm_booking_table_name}\'" ) != $mm_booking_table_name ) {
        $sql = "CREATE TABLE $mm_booking_table_name (
            id mediumint(9) NOT NULL AUTO_INCREMENT,
            first_name varchar(255),
            last_name varchar(255),
            address varchar(255),
            lat decimal(10, 8) NOT NULL,
            lng decimal(11, 8) NOT NULL,
            field enum(\'option1\', \'option2\', \'option3\'),
            phone_number varchar(255),
            email varchar(255),
            date_time_from datetime DEFAULT CURRENT_TIMESTAMP,
            date_time_to datetime DEFAULT CURRENT_TIMESTAMP,
      status enum(\'pending\', \'approved\', \'rejected\') DEFAULT \'pending\',
      modification_time datetime ON UPDATE CURRENT_TIMESTAMP,
      creation_time datetime DEFAULT CURRENT_TIMESTAMP,
      PRIMARY KEY  (id)
    ) $charset_collate;";

        require_once( ABSPATH . \'wp-admin/includes/upgrade.php\' );
        dbDelta( $sql );
        add_option( \'mm_booking_db_version\', $mm_booking_db_version );
    }
}
register_activation_hook( __FILE__, \'mm_booking_activate\' );

相关推荐

$wpdb->GET_RESULTS()足够安全吗

原始问题如果我使用get\\u results()函数,这对mysql注入攻击足够安全吗?global $wpdb; $wpdb->get_results("select * from tableA where B = C"); 还是应该先做些准备?最佳做法是什么?在2021 12月的今天,我们应该如何做到这一点?更新,所以我应该用这个来代替?global $wpdb; $my_variable = "sometext"; $p