WordPress.org

Ready to get started?Download WordPress

Forums

Creating a plugin; create table on activation (2 posts)

  1. advancedstyle
    Member
    Posted 4 years ago #

    I have the following code setup to create a table when the plugin is activated, but no table gets created.

    Notes:
    - When i copy the SQL direct to phpmyadmin it works
    - The database user does have permission to create tables.

    $scores_table_name = $wpdb->prefix . "scorepredictions";
    $scores_db_version = "1.0";
    
    function scores_install () {
       global $wpdb;
       global $scores_db_version;
       global $scores_table_name;
    
       if($wpdb->get_var("show tables like '$scores_table_name'") != $scores_table_name) {
    
          $sql = "CREATE TABLE " . $scores_table_name . " (
    	  id mediumint(9) NOT NULL AUTO_INCREMENT,
    	  post_id int NOT NULL,
    	  user_id int NOT NULL,
    	  home_team VARCHAR(255) NOT NULL,
    	  away_team VARCHAR(255) NOT NULL,
    	  home_score int NOT NULL,
    	  away_score int NOT NULL,
    	  UNIQUE KEY id (id)
    	);";
          require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
    
          dbDelta($sql);
    
          add_option("scores_db_version", $scores_db_version);
    
       }
    }
    
    register_activation_hook(__FILE__,'scores_install');

    Any ideas as to what i'm doing wrong?

  2. JonLambell
    Member
    Posted 3 years ago #

    Did you ever get this sorted? I'm stuck with the exact same problem and responses around here just don't seem to be happening, so I'm guessing it's some kind of bug that people don't know how to fix.

Topic Closed

This topic has been closed to new replies.

About this Topic