使用插件创建表不起作用

时间:2014-12-03 作者:lost_in_magento

在阅读了许多问题和答案后,许多人问了同样的问题,我一直遵循着与指导人员相同的指导,但我的坏消息是,我使用的是无效的

dbDelta($sql)$wpdb->查询($sql)

    它不工作

    示例代码

    <?php
    /*
    Plugin Name: ContactUs
    Plugin URI: 
    Description: Simple Plugin Developed for Testing purpose
    Version: 1.0.0
    Author: rajeshw
    Author URI: codelisense.com
    */  
    
    //Creating table
        function contact_installation(){
            global $wpdb;
            $table_name = $wpdb->prefix. "contactus";
    
            if($wpdb->get_var (\'SHOW TABLES LIKE\' .$table_name ) != $table_name){
                $sql= "CREATE TABLE" .$table_name. "(
                       id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, 
                       firstname VARCHAR(30) NOT NULL,
                       lastname VARCHAR(30) NOT NULL,
                       email VARCHAR(50),
                       reg_date TIMESTAMP
                       )";
                require_once (ABSPATH. \'wp-admin/includes/upgrade.php\' );
                $wpdb->query($sql);
    
                //had replaced the above line with this line
                dbDelta($sql);
            }
        }
        register_activation_hook(__FILE__, \'contact_installation\');
    
    尚未创建表。。。请帮忙

2 个回复
最合适的回答,由SO网友:shanebp 整理而成

你的点引号符号很时髦。尝试以下操作:

if($wpdb->get_var("SHOW TABLES LIKE \'$table_name\'" ) != $table_name){
    $sql= "CREATE TABLE $table_name (
           id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, 
           firstname VARCHAR(30) NOT NULL,
           lastname VARCHAR(30) NOT NULL,
           email VARCHAR(50),
           reg_date TIMESTAMP
           );";
    require_once (ABSPATH. \'wp-admin/includes/upgrade.php\' );
    dbDelta($sql);
}

SO网友:deemi-D-nadeem

请尝试此代码。

创建一个类似“my\\u db.php”的文件并粘贴以下代码:

global $table_version;
$table_version = "1.0";

function func_table_contactus(){
    creat_table_contactus();
}

function creat_table_contactus(){
    global $wpdb;
    global $table_version;

    $table_contactus = $wpdb->prefix."contactus";
    $slider_ver = get_option("table_version");

    $check_table_contactus = $wpdb->get_var("SHOW TABLES LIKE \'$table_contactus\'");

    if($check_table_contactus != $table_contactus ||  $slider_ver != $table_version ) {
         $sql= "CREATE TABLE" .$table_name. "(
                   id INT(6) UNSIGNED NOT NULL AUTO_INCREMENT, 
                   firstname VARCHAR(30) NOT NULL,
                   lastname VARCHAR(30) NOT NULL,
                   email VARCHAR(50),
                   reg_date TIMESTAMP
           PRIMARY KEY (id)
               )";

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

    update_option("table_version", $table_version);
    }
    else{
        add_option("table_version", $table_version);
    }

}
在插件主文件中

<?php
/*
Plugin Name: ContactUs
Plugin URI: 
Description: Simple Plugin Developed for Testing purpose
Version: 1.0.0
Author: rajeshw
Author URI: codelisense.com
*/  

require "my_db.php";
    add_action(\'plugins_loaded\', \'func_table_contactus\');
希望您能找到解决方案

结束

相关推荐

扩展WP_LIST_TABLE似乎在使用$This->Get_Columns()时出错--出什么问题了?

我正在尝试为view page of a plugin of mine 通过以下教程:Create Native Admin Tables In WordPress The Right Way  ;作者:Jeremy Desvaux de Marigny  ;粉碎杂志。下面是我如何扩展WP_List_Table 为我的插件初始化。(Code at Pastebin)问题是:它不显示任何列或行,只显示表的顶部和底部。是的var_dump() 步骤,并在中发现Line 149