Problem in creating table through plugin code

I’m using the following code to create the database table using plugin.
However, the table is neither getting created nor I’m getting any error message.
The code is as follows:

    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);

I have followed the rules as mentioned the WordPress Codex.
Please help!!
Thanks in advance.

Related posts

Leave a Reply

1 comment

  1. The above code should be placed in a function and then hook that with plugin activation hook? Here’s sample structure of plugin-name.php file.

    Note –

    • Use CREATE TABLE IF NOT EXISTS instead only create table

    Code Updated on – 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');
    
    ?>
    

    Reference –

    • Register activation hook – Use this to do some action when plugin is activated, In this case we’r calling install_my_plugin() function to create table.