通过插件代码创建表时出现问题

时间:2012-08-03 作者:Pooja

我使用以下代码使用插件创建数据库表。但是,该表既没有创建,也没有收到任何错误消息。代码如下:

    global $wpdb;
    $table_name = "tbllb_club";

    $sql = "create table $table_name (
    club_id int(11) NOT NULL auto_increment,
    project_name varchar(500) NOT NULL,
    first_name varchar(100) NOT NULL,
    school_status varchar(50) NOT NULL,
    school_name varchar(200) NOT NULL,
    school_city varchar(50) NOT NULL,
    school_state varchar(50) NOT NULL,
    year_in_school varchar(50) NOT NULL,
    age int(2) NOT NULL,
    email varchar(200) NOT NULL,
    mobile varchar(15) NOT NULL,
    chat_id  varchar(50) NOT NULL,
    facebook_page_link varchar(200) NOT NULL,
    mailing_address_street varchar(50) NOT NULL,
    mailing_address_city varchar(50) NOT NULL,
    mailing_address_state varchar(50) NOT NULL,
    mailing_address_zip char(6) NOT NULL,
    social_networking varchar(50) NOT NULL,
    organization_involvement varchar(2000) NOT NULL,
    yesclub_heard varchar(50) NOT NULL,
    volunteer_experience varchar(2000) NOT NULL,
    volunteer_reason varchar(2000) NOT NULL,
    skills varchar(2000) NOT NULL,
    creative_ideas varchar(5000) NOT NULL,
    other_information varchar(2000) NOT NULL,
    face_photo_url varchar(200),
    school_image_url varchar(200),
    video_clip_url varchar(200),
    PRIMARY KEY  (club_id)
    );";

    require_once(ABSPATH . \'wp-admin/includes/upgrade.php\');
    dbDelta($sql);
我遵守了Wordpress Codex中提到的规则。请帮忙!!提前谢谢。

1 个回复
SO网友:amit

上面的代码应该放在一个函数中,然后用插件激活钩子钩住它?以下是的示例结构plugin-name.php 文件

注-使用CREATE TABLE IF NOT EXISTS 相反,仅限create table
    代码更新日期:2012-08-04

    <?php // this code goes into your my_plugin_name.php file
    
    function install_my_plugin() {
    
        global $wpdb;
        $table_name = "tbllb_club";
    
        //the SQL commands to create table
        $sql = "SQL_QUERY_1";
        $sql .= "SQL_QUERY_2";
    
        //this stuff is needed to create the table in MySQL database
        require_once(ABSPATH . \'wp-admin/includes/upgrade.php\');
        dbDelta($sql);
    
        //optional 
        add_option("my_plugin_version", "1.0");
    }
    
    //lets get it hooked to plugin activation
    register_activation_hook(__FILE__,\'install_my_plugin\');
    
    ?>
    
    参考文献Register activation hook - 当插件被激活时,使用此按钮执行一些操作,在本例中,我们正在调用install_my_plugin() 函数创建表

结束