• Resolved teamplaylotto

    (@teamplaylotto)


    I am trying to add a table to the database upon activation of my plugin but it’s not adding it. also, it’s not storing the value of a global variable with add_option. please help!

    here is my code

    function atf_install() {
       global $wpdb;
       global $atf_db_version;
       add_option("atf_db_version", $atf_db_version);
    
       $table_name = $wpdb->prefix . "wptwitipid";
       if($wpdb->get_var("show tables like '$table_name'") != $table_name) {
          $sql = "CREATE TABLE " . $table_name . " (
          id mediumint(9) NOT NULL AUTO_INCREMENT,
          email varchar(120) NOT NULL,
          twitid varchar(120) NOT NULL,
          KEY email (email)
          );";
          require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
          dbDelta($sql);
       }
    }
    $atf_db_version = "1.0";
    register_activation_hook(__FILE__, 'atf_install');

Viewing 1 replies (of 1 total)
  • Thread Starter teamplaylotto

    (@teamplaylotto)

    fixed it with this

    function atf_install() {
       global $wpdb;
       $table_name = $wpdb->prefix . "wptwitipid";
       if($wpdb->get_var("show tables like '$table_name'") != $table_name) {
          $sql = "CREATE TABLE " . $table_name . " (
          id mediumint(9) NOT NULL AUTO_INCREMENT,
          email varchar(120) NOT NULL,
          twitid varchar(120) NOT NULL,
          PRIMARY KEY  (id),
          KEY (email)
          );";
          require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
          dbDelta($sql);
       }
    }

    I needed to set the auto increment key as primary and add an extra space.

Viewing 1 replies (of 1 total)
  • The topic ‘adding a table to db with dbDelta not working on activation’ is closed to new replies.