在安装时创建数据库表并插入not working - wordpress插件

时间:2018-06-18 12:50:24

标签: wordpress

我创建了一个在激活时在Wordpress数据库中创建表的函数。我已经检查过只有这个函数在激活时会产生意外的输出错误。同样奇怪的是代码只创建并填充第一个表。但是当我删除创建第一个表的代码部分时,以下sql查询不会生效。我通读了互联网上的所有相关帖子,但没有找到解决方案。任何人都可以帮助我吗?

    function prtxgal_install(){
    global $wpdb;

    $prtx_getsettings = $wpdb->get_row( "SELECT * FROM " . $wpdb->prefix . "prtxgal_settings WHERE id = 1;" );


    if ($prtx_getsettings == NULL) {

        $table_name = $wpdb->prefix . "prtxgal_settings";
        $table_name2 = $wpdb->prefix . "prtxgal_galleries";
        $table_name3 = $wpdb->prefix . "prtxgal_images";

        $charset_collate = $wpdb->get_charset_collate();

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

        $sql = "CREATE TABLE $table_name (
        id mediumint(9) NOT NULL AUTO_INCREMENT,
        time timestamp DEFAULT CURRENT_TIMESTAMP NOT NULL,
        outline_color text NOT NULL,
        desc_bg_color text NOT NULL,
        font_color text NOT NULL,
        file_path text NOT NULL,
        PRIMARY KEY  (id)
        ) $charset_collate;";
        dbDelta( $sql );

        $sql = "CREATE TABLE $table_name2 (
        id mediumint(9) NOT NULL AUTO_INCREMENT,
        time timestamp DEFAULT CURRENT_TIMESTAMP NOT NULL,
        name text NOT NULL,
        preview_image text NOT NULL,
        image_number text NOT NULL,
        order int(10) NOT NULL,
        published int(2) NOT NULL,
        PRIMARY KEY  (id)
        ) $charset_collate;";
        dbDelta( $sql );

        $sql = "CREATE TABLE $table_name3 (
        id mediumint(9) NOT NULL AUTO_INCREMENT,
        time timestamp DEFAULT CURRENT_TIMESTAMP NOT NULL,
        gallery_id text NOT NULL,
        filename text NOT NULL,
        image_url text NOT NULL,
        thumb_url text NOT NULL,
        description text NOT NULL,
        alt text NOT NULL,
        order int(10) NOT NULL,
        PRIMARY KEY  (id)
        ) $charset_collate;";
        dbDelta( $sql );

        $wpdb->insert(
                $table_name,
                array(
                        'id' => 1,
                        'outline_color' => '#000000',
                        'desc_bg_color' => '#000000',
                        'font_color' => '#ffffff',
                        'file_path' => '/images/default.png'
                    )
                    );

    }

4 个答案:

答案 0 :(得分:0)

尝试下面的代码问题是订单字段名称,而运行sql订单字则被视为订单关键字,因为这是问题所在。

{{1}}

答案 1 :(得分:0)

您没有提及错误消息,但我认为这是问题所在。

一旦激活插件就会创建表,您的SQL语句可能会创建现有表。将“CREATE TABLE $table_nameX”更改为“CREATE TABLE IF NOT EXISTS $table_nameX”,然后再次尝试使用您的代码。

答案 2 :(得分:0)

我通过将查询直接粘贴到PHP MyAdmin来部分解决了这个问题。问题是我可能在列名中使用了禁用词,即“order”。我将其更改为'img_order',现在所有表都正确创建了:)但我仍然得到“插件生成226个字符的意外输出...”错误

答案 3 :(得分:0)

我最终得到了这个并且运作良好,非常感谢您的帮助。

function prtxgal_install(){
    global $wpdb;



        $table_name = $wpdb->prefix . "prtxgal_settings";
        $table_name2 = $wpdb->prefix . "prtxgal_galleries";
        $table_name3 = $wpdb->prefix . "prtxgal_images";

        $charset_collate = $wpdb->get_charset_collate();

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

        $sql = "CREATE TABLE IF NOT EXISTS $table_name (
        id mediumint(9) NOT NULL AUTO_INCREMENT,
        time timestamp DEFAULT CURRENT_TIMESTAMP NOT NULL,
        outline_color text NOT NULL,
        desc_bg_color text NOT NULL,
        font_color text NOT NULL,
        file_path text NOT NULL,
        PRIMARY KEY  (id)
        ) $charset_collate;";
        dbDelta( $sql );

        $sql = "CREATE TABLE IF NOT EXISTS $table_name2 (
        id mediumint(9) NOT NULL AUTO_INCREMENT,
        time timestamp DEFAULT CURRENT_TIMESTAMP NOT NULL,
        name text NOT NULL,
        preview_image text NOT NULL,
        image_count text NOT NULL,
        gal_order int(10) NOT NULL,
        published int(9) NOT NULL,
        PRIMARY KEY  (id)
        ) $charset_collate;";
        dbDelta( $sql );

        $sql = "CREATE TABLE IF NOT EXISTS $table_name3 (
        id mediumint(9) NOT NULL AUTO_INCREMENT,
        time timestamp DEFAULT CURRENT_TIMESTAMP NOT NULL,
        gallery_id text NOT NULL,
        filename text NOT NULL,
        image_url text NOT NULL,
        thumb_url text NOT NULL,
        description text NOT NULL,
        alt text NOT NULL,
        img_order int(10) NOT NULL,
        PRIMARY KEY  (id)
        ) $charset_collate;";
        dbDelta( $sql );

        $wpdb->insert(
                $table_name,
                array(
                        'id' => 1,
                        'outline_color' => '#000000',
                        'desc_bg_color' => '#000000',
                        'font_color' => '#ffffff',
                        'file_path' => '/images/default.png'
                    )
                    );




}