0

So I'm trying to set up a database to use for displaying purposes in the add_menu_page(). I'm using this on a custom parent theme

Now I have this so far (currently in my functions.php):

function countriesDB_init()
{
    global $wpdb;
    $table_name = $wpdb->prefix . 'designitcountries';

    //create table (if it doesnt exist)

    $charset_collate = $wpdb->get_charset_collate();

    $sql = "CREATE TABLE" . " " . $table_name . "(
  id mediumint(9) NOT NULL AUTO_INCREMENT,
  time datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
  name tinytext NOT NULL,
  text text NOT NULL,
  url varchar(55) DEFAULT '' NOT NULL,
  PRIMARY KEY  (id)
) $charset_collate;";

    require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
    dbDelta( $sql );

        $name = 'Sukru Kintas';
        $title = 'President';
        $country = 'World all hapkido association';

        $wpdb->insert(
            $table_name,
            array(
                'time' => current_time('mysql'),
                'title' => $title,
                'name' => $name,
                'country' => $country
            )
        );
}
do_action( 'after_switch_theme', 'countriesDB_init' );

It should create a database table when it does not exist yet. But it's not doing anything. No database table has been created.

WP_DEBUG log

WordPress database-fout: [Table 'ID203293_lacroix.wp_designitcountries' doesn't exist]
SELECT COUNT(word) AS occurrence, word FROM `wp_designitcountries` GROUP BY word ORDER BY occurrence DESC

WordPress database-fout: [Table 'ID203293_lacroix.wp_designitcountries' doesn't exist]
SELECT COUNT(word) AS occurrence, word FROM `wp_designitcountries` GROUP BY word ORDER BY occurrence DESC

WordPress database-fout: [Table 'ID203293_lacroix.wp_designitcountries' doesn't exist]
SELECT COUNT(word) AS occurrence, word FROM `wp_designitcountries` GROUP BY word ORDER BY occurrence DESC

WordPress database-fout: [Table 'ID203293_lacroix.wp_designitcountries' doesn't exist]
SELECT COUNT(word) AS occurrence, word FROM `wp_designitcountries` GROUP BY word ORDER BY occurrence DESC

WordPress database-fout: [Table 'ID203293_lacroix.wp_designitcountries' doesn't exist]
SELECT COUNT(word) AS occurrence, word FROM `wp_designitcountries` GROUP BY word ORDER BY occurrence DESC

WordPress database-fout: [Table 'ID203293_lacroix.wp_designitcountries' doesn't exist]
SELECT COUNT(word) AS occurrence, word FROM `wp_designitcountries` GROUP BY word ORDER BY occurrence DESC
  • your code is hard to understand. what does the function dbDelta? please show us error messages (set WP_DEBUG to true in the wp-config.php) – s t May 1 '17 at 10:17
  • dbDelta is a function within Wordpress itself : developer.wordpress.org/reference/functions/dbdelta – CompactCode May 1 '17 at 10:27
  • log is just saying that the table does not exist. Like i said its not being registred – CompactCode May 1 '17 at 11:18
2

dbDelta() is very picky, below I've checked the requirements for dbDelta() in your code. As you can see below your code fails two of the requirements which most likely prevents dbDelta() from creating the tables.

  • You must put each field on its own line in your SQL statement. CHECK
  • You must have two spaces between the words PRIMARY KEY and the definition of your primary key. CHECK
  • You must use the key word KEY rather than its synonym INDEX and you must include at least one KEY. N/A
  • KEY must be followed by a SINGLE SPACE then the key name then a space then open parenthesis with the field name then a closed parenthesis. N/A
  • You must not use any apostrophes or backticks around field names. CHECK
  • Field types must be all lowercase. FAIL
  • SQL keywords, like CREATE TABLE and UPDATE, must be uppercase. CHECK
  • You must specify the length of all fields that accept a length parameter. int(11), for example. FAIL


Also

From the documentation:

The dbDelta function examines the current table structure, compares it to the desired table structure, and either adds or modifies the table as necessary

Therefor your initial check if($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name) { is redundant as dbDelta() does that for you.

  • I updated my code with what you said . Thanks for all the info. Seems that i overlooked some things. But its still not adding a database. I do switch my theme to another and back since the action hook says that – CompactCode May 1 '17 at 11:45
  • So also i can not use the hook that i putted on there , changing it to add_action ('init' , 'countriesDB'); got it also to work. But i have your post as an answer because most people will have that problem – CompactCode May 1 '17 at 20:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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