2

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.

2
  • not an answer to your question, but I'm curious why you are not just using a custom post type + meta data for this? you get the WordPress API to deal with your data and don't have to write SQL queries from scratch.
    – Milo
    Commented Aug 4, 2012 at 14:58
  • I will try this option also... Thanks a lot Milo for the suggestion.
    – Pooja
    Commented Aug 6, 2012 at 21:35

1 Answer 1

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.
5
  • Thanks Amit for the reply. I have done in the same way you have shown here but it is not working. I'm creating 2 tables using the same technique. One table is getting created but this table is not getting created. When I try to manually create this table in phpMyAdmin, it is created successfully.
    – Pooja
    Commented Aug 4, 2012 at 13:48
  • using same plugin to create two tables ?
    – Amit Kosti
    Commented Aug 4, 2012 at 13:59
  • sorry, you are incorrect here: $sql = "$variable"; is perfectly valid php, double-quoted vars are expanded.
    – Milo
    Commented Aug 4, 2012 at 14:57
  • Thanks for pointing out, Thats My mistake. Now updated the code.
    – Amit Kosti
    Commented Aug 4, 2012 at 15:05
  • Thanks a lot Amit. Now it is working fine with the solution provided by you.
    – Pooja
    Commented Aug 6, 2012 at 21:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.