0

My plugin is supposed to create two tables upon activation. However only the second table is created. Here is the part of my function that creates the tables.

require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
global $wpdb;
$ml_wpid = $wpdb->prefix . "ml_wpid";
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE IF NOT EXISTS " . $ml_wpid . " (
    `wp_id` mediumint(9) NOT NULL
    `name` text NOT NULL
UNIQUE (`char_id`)
) $charset_collate;";
dbDelta( $sql );

require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
global $wpdb;
$ml_char = $wpdb->prefix . "ml_char";
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE IF NOT EXISTS " . $ml_char . " (
    `char_id` mediumint(9) NOT NULL AUTO_INCREMENT,
    `name` text NOT NULL,
UNIQUE (`char_id`)
) $charset_collate;";
dbDelta( $sql );

// starts output buffering
ob_start();

I feel that the reason this is happening is because the $sql variable is being redefined. However I've tried renaming the second table's $sql variable to something different like $sql2 but nothing different happened. Any suggestions? Thanks in advance!

1 Answer 1

1

Having a similar issue my first table works but the second does not.

function Create_Tables_Super_Farm()
{

global $wpdb;

$charset_collate = $wpdb->get_charset_collate();

// Calendar Events
$table_name = $wpdb->prefix . "calendarevents"; 
$table_name2 = $wpdb->prefix . "changelogs"; 
$sql = "CREATE TABLE IF NOT EXISTS $table_name (
  ID int(11) NOT NULL AUTO_INCREMENT,
  timestamp datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
  TypeID int(11) NOT NULL,
  CustomerID int(11) NOT NULL,
  SeedID int(11) NOT NULL,
  Date datetime NOT NULL,
  Title varchar(250) NOT NULL,
  Description varchar(500) DEFAULT NULL,
  PRIMARY KEY (ID),
  UNIQUE KEY ID_UNIQUE (ID)
) $charset_collate ;";
$sql2 = "CREATE TABLE IF NOT EXISTS $table_name2 (
  ID int(11) NOT NULL AUTO_INCREMENT,
  timestamp datetime DEFAULT CURRENT_TIMESTAMP,
  EmployeeID int(11) DEFAULT NULL,
  TableID int(11) DEFAULT NULL,
  Table varchar(500) DEFAULT NULL,
  Field varchar(500) DEFAULT NULL,
  Before varchar(5000) DEFAULT NULL,
  After varchar(5000) DEFAULT NULL,
  PRIMARY KEY (ID),
  UNIQUE KEY ID_UNIQUE (ID)
) $charset_collate ;";
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
dbDelta( $sql );
dbDelta( $sql2 );
}

Try removing the "`" char's I could not get anything to work before I did that. Overwriting the $sql variable should not cause an issue. Best I can tell dbDelta is very finicky and I am missing a space or something. Can not find it so far any help would be great.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.