techy
techy

Reputation: 544

Wordpress Plugin Creating Database Tables

I am currently working on a plugin for personal use, but I just can't get the plugin to create a table, I try activation the plugin but it doesn't create a table here is my code:

team-monobattle-registraion.php:

<?php
/*
Plugin Name: Team Monobattle Registration
Plugin URI: http://www.web-geeks.com/
Description: Plugin for registering teams for monobattles.
Author: Web-geeks
Version: 1.0
Author URI: http://www.web-geeks.com/
*/

function tmbr_admin() {
    include('team-monobattle-admin.php');
}

function tmbr_admin_actions() {
    add_options_page('Team Monobattle Registration', 'Team Monobattle Registration', 'manage_options', 'tmbr', 'tmbr_admin');
}

add_action('admin_menu', 'tmbr_admin_actions');

include('team-monobattle-database.php');

register_activation_hook(__FILE__, 'tmbr_install');
?>

team-monobattle-database.php:

<?php

global $tmbr_db_table_version;
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
$tmbr_db_table_version = "1.0";
global $wpdb;

function tmbr_install()
{

    global $tmbr_db_table_version;
    $installed_ver = get_option( "tmbr_db_table_version" );

    if($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != 'wp_teammonobattleteams' ||  $installed_ver != $tmbr_db_table_version ) {

    $sql = "CREATE TABLE wp_teammonobattleteams (
    teamid mediumint(9) NOT NULL AUTO_INCREMENT,
    teamname tinytext NOT NULL,
    pname1 tinytext NOT NULL,
    pid1 smallint NOT NULL,
    pname2 tinytext NOT NULL,
    pid2 smallint NOT NULL,
    pname3 tinytext NOT NULL,
    pid3 smallint NOT NULL,
    pname4 tinytext NOT NULL,
    pid4 smallint NOT NULL,
    UNIQUE KEY id (id)
    );";

    dbDelta($sql);

    echo('installed');
    }

}
?>

team-monobattle-admin.php:

<div class="wrap">
<?php    echo "<h2>" . __( 'Team Monobattle Admin', 'tmbr_admin' ) . "</h2>"; ?>
</div>

Help appreciated.

Upvotes: 0

Views: 2047

Answers (2)

user3094069
user3094069

Reputation:

" CREATE TABLE " is not safe to use. Because if the table already exist it will be a error in your mysql query. also which reports un avoidable error. So try to use " CREATE TABLE IF NOT EXISTS" . That's good practice.

If you have any problem in table creation read some online tutorials and create one. Here is a link for your reference.

http://kvcodes.com/2014/06/how-to-create-table-in-wordpress-database/

Upvotes: 0

Alexcp
Alexcp

Reputation: 333

The variables have to be inside your function.

Upvotes: 1

Related Questions