最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

wpdb - Problem with creating tables for Plugin

programmeradmin0浏览0评论

I hope someone can help me.

I try to execute this code to install the tables for my Plugin. But for some reason it doesn't work (Doesn't appear in my wordpress database) and I can't find out why. Can someone help me?

<?php
/*
    Plugin Name: Test Plugin 
    PLUGIN URL: 
    Description: Dashboard fuer die Kundenverwaltung
    Version: 1.0
    Author: Maxime Buelow
*/
function activate(){
        global $wpdb;
        
        $charset_collate = $wpdb->get_charset_collate();
        
        require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
        
        //$verb = new mysqli('localhost','root','root','test4');
        $table_name1 = $wpdb->prefix . "Veranstaltung";
        $table_name2 = $wpdb->prefix . "Personen";
        $table_name3 = $wpdb->prefix . "Sponsor";
        $table_name4 = $wpdb->prefix . "sponkunde";
        
        //$wpdb->query("CREATE DATABASE eBagDB");
        $query1 = "CREATE TABLE " . $table_name1 . "(
        ver_id int(11) NOT NULL AUTO_INCREMENT, 
        VeranstaltungName varchar(255), 
        PRIMARY KEY  (ver_id)
        ) $charset_collate;";
        $query2 = "CREATE TABLE " . $table_name2 . "(
        pers_id int(11) NOT NULL AUTO_INCREMENT, 
        Datum DATE, 
        Passwort varchar(255) UNIQUE, 
        Vorname varchar(255), 
        Nachname varchar(255), 
        Anrede varchar(255), 
        LoeschDatum DATE, 
        fkVer_id int(11), 
        FOREIGN KEY(fkVer_id) REFERENCES Veranstaltung(Ver_id), 
        PRIMARY KEY  (pers_id)
        ) $charset_collate;";
        $query3 = "CREATE TABLE " . $table_name3 . "(
        sp_id int(11) NOT NULL AUTO_INCREMENT, 
        Code varchar(255), 
        Name varchar(255), 
        PRIMARY KEY  (sp_id)
        ) $charset_collate;";
        $query4 = "CREATE TABLE " . $table_name4 . "(
        fkPers_id int(11), 
        fkSp_id int(11), 
        FOREIGN KEY(fkPers_id) REFERENCES Personen(pers_id), 
        FOREIGN KEY(fkSp_id) REFERENCES sponsor(sp_id)
        ) $charset_collate;"; 
        
        /*$wpdb->query($query1);
        $wpdb->query($query2);
        $wpdb->query($query3);
        $wpdb->query($query4);*/
        
        //$ergCrDB = $wpdb->get_results($createDB, OBJECT);
        
        //$ergCrTbVer = $wpdb->get_results($createTbVer, OBJECT);
        /*$ergCrTbPers = $wpdb->get_results($createTbPers, OBJECT);
        $ergCrTbSp = $wpdb->get_results($createTbPersSp, OBJECT);
        $ergCrTbPersSp = $wpdb->get_results($createTbSponsor, OBJECT);*/
        
        dbDelta($query1);
        dbDelta($query2);
        dbDelta($query3);
        dbDelta($query4);
}
register_activation_hook(__FILE__,'activate');?>

UPDATE

The issue has been partly resolved so far. In my wp-config was the wrong database chosen. Thanks for the help so far! :)

I got another question: Now the table creation works, but only the first table is created. The other 3 not. Does somebody know why?

My Code now looks like this:

function artok_activate(){
    global $wpdb;
    require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
    $charset_collate                = $wpdb->get_charset_collate();
    $table_Veranstaltung            = $wpdb->prefix . 'Veranstaltung';
    $table_Personen                 = $wpdb->prefix . 'Personen';
    $table_Sponsor                  = $wpdb->prefix . 'Sponsor';
    $table_SponKunde                = $wpdb->prefix . 'SponKunde';
    
    if( $wpdb->get_var( "SHOW TABLES LIKE '$table_Veranstaltung'" ) != $table_Veranstaltung ) {
        $tableVer_Crea = 'CREATE TABLE ' . $table_Veranstaltung . '(
        ver_id int(11) NOT NULL AUTO_INCREMENT, 
        VeranstaltungName varchar(255), 
        PRIMARY KEY  (ver_id)
        )' . $charset_collate . ';';
        dbDelta( $tableVer_Crea );
        $success = empty($wpdb->last_error);
        
        return $success;
    }
    
    if( $wpdb->get_var( "SHOW TABLES LIKE '$table_Personen'" ) != $table_Personen ) {
        $tablePers_Crea = "CREATE TABLE " . $table_Personen . "(
        pers_id int(11) NOT NULL AUTO_INCREMENT, 
        Datum DATE, 
        Passwort varchar(255) UNIQUE, 
        Vorname varchar(255), 
        Nachname varchar(255), 
        Anrede varchar(255), 
        LoeschDatum DATE, 
        fkVer_id int(11), 
        FOREIGN KEY(fkVer_id) REFERENCES wp_Veranstaltung(Ver_id), 
        PRIMARY KEY  (pers_id)
        )" . $charset_collate . ";";
        
        dbDelta($tablePers_Crea);
        
        $success = empty($wpdb->last_error);
        
        return $success;
    }
    
    if( $wpdb->get_var( "SHOW TABLES LIKE '$table_Sponsor'" ) != $table_Sponsor) {
        $tableSp_Crea = "CREATE TABLE " . $table_Sponsor . "(
        sp_id int(11) NOT NULL AUTO_INCREMENT, 
        Code varchar(255), 
        Name varchar(255), 
        PRIMARY KEY  (sp_id)
        )" . $charset_collate . ";";
        dbDelta( $tableSp_Crea );
        $success = empty($wpdb->last_error);
        
        return $success;
    }
    
    if( $wpdb->get_var( "SHOW TABLES LIKE '$table_SponKunde'" ) != $table_SponKunde ) {
        $tableSpKu_Crea = "CREATE TABLE " . $table_SponKunde . "(
        fkPers_id int(11), 
        fkSp_id int(11), 
        FOREIGN KEY(fkPers_id) REFERENCES wp_Personen(pers_id), 
        FOREIGN KEY(fkSp_id) REFERENCES wp_Sponsor(sp_id)
        )" . $charset_collate . ";";
        dbDelta( $tableSpKu_Crea );
        $success = empty($wpdb->last_error);
        
        return $success;
    }
}   
    register_activation_hook( __FILE__, 'artok_activate' );

I hope someone can help me.

I try to execute this code to install the tables for my Plugin. But for some reason it doesn't work (Doesn't appear in my wordpress database) and I can't find out why. Can someone help me?

<?php
/*
    Plugin Name: Test Plugin 
    PLUGIN URL: 
    Description: Dashboard fuer die Kundenverwaltung
    Version: 1.0
    Author: Maxime Buelow
*/
function activate(){
        global $wpdb;
        
        $charset_collate = $wpdb->get_charset_collate();
        
        require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
        
        //$verb = new mysqli('localhost','root','root','test4');
        $table_name1 = $wpdb->prefix . "Veranstaltung";
        $table_name2 = $wpdb->prefix . "Personen";
        $table_name3 = $wpdb->prefix . "Sponsor";
        $table_name4 = $wpdb->prefix . "sponkunde";
        
        //$wpdb->query("CREATE DATABASE eBagDB");
        $query1 = "CREATE TABLE " . $table_name1 . "(
        ver_id int(11) NOT NULL AUTO_INCREMENT, 
        VeranstaltungName varchar(255), 
        PRIMARY KEY  (ver_id)
        ) $charset_collate;";
        $query2 = "CREATE TABLE " . $table_name2 . "(
        pers_id int(11) NOT NULL AUTO_INCREMENT, 
        Datum DATE, 
        Passwort varchar(255) UNIQUE, 
        Vorname varchar(255), 
        Nachname varchar(255), 
        Anrede varchar(255), 
        LoeschDatum DATE, 
        fkVer_id int(11), 
        FOREIGN KEY(fkVer_id) REFERENCES Veranstaltung(Ver_id), 
        PRIMARY KEY  (pers_id)
        ) $charset_collate;";
        $query3 = "CREATE TABLE " . $table_name3 . "(
        sp_id int(11) NOT NULL AUTO_INCREMENT, 
        Code varchar(255), 
        Name varchar(255), 
        PRIMARY KEY  (sp_id)
        ) $charset_collate;";
        $query4 = "CREATE TABLE " . $table_name4 . "(
        fkPers_id int(11), 
        fkSp_id int(11), 
        FOREIGN KEY(fkPers_id) REFERENCES Personen(pers_id), 
        FOREIGN KEY(fkSp_id) REFERENCES sponsor(sp_id)
        ) $charset_collate;"; 
        
        /*$wpdb->query($query1);
        $wpdb->query($query2);
        $wpdb->query($query3);
        $wpdb->query($query4);*/
        
        //$ergCrDB = $wpdb->get_results($createDB, OBJECT);
        
        //$ergCrTbVer = $wpdb->get_results($createTbVer, OBJECT);
        /*$ergCrTbPers = $wpdb->get_results($createTbPers, OBJECT);
        $ergCrTbSp = $wpdb->get_results($createTbPersSp, OBJECT);
        $ergCrTbPersSp = $wpdb->get_results($createTbSponsor, OBJECT);*/
        
        dbDelta($query1);
        dbDelta($query2);
        dbDelta($query3);
        dbDelta($query4);
}
register_activation_hook(__FILE__,'activate');?>

UPDATE

The issue has been partly resolved so far. In my wp-config was the wrong database chosen. Thanks for the help so far! :)

I got another question: Now the table creation works, but only the first table is created. The other 3 not. Does somebody know why?

My Code now looks like this:

function artok_activate(){
    global $wpdb;
    require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
    $charset_collate                = $wpdb->get_charset_collate();
    $table_Veranstaltung            = $wpdb->prefix . 'Veranstaltung';
    $table_Personen                 = $wpdb->prefix . 'Personen';
    $table_Sponsor                  = $wpdb->prefix . 'Sponsor';
    $table_SponKunde                = $wpdb->prefix . 'SponKunde';
    
    if( $wpdb->get_var( "SHOW TABLES LIKE '$table_Veranstaltung'" ) != $table_Veranstaltung ) {
        $tableVer_Crea = 'CREATE TABLE ' . $table_Veranstaltung . '(
        ver_id int(11) NOT NULL AUTO_INCREMENT, 
        VeranstaltungName varchar(255), 
        PRIMARY KEY  (ver_id)
        )' . $charset_collate . ';';
        dbDelta( $tableVer_Crea );
        $success = empty($wpdb->last_error);
        
        return $success;
    }
    
    if( $wpdb->get_var( "SHOW TABLES LIKE '$table_Personen'" ) != $table_Personen ) {
        $tablePers_Crea = "CREATE TABLE " . $table_Personen . "(
        pers_id int(11) NOT NULL AUTO_INCREMENT, 
        Datum DATE, 
        Passwort varchar(255) UNIQUE, 
        Vorname varchar(255), 
        Nachname varchar(255), 
        Anrede varchar(255), 
        LoeschDatum DATE, 
        fkVer_id int(11), 
        FOREIGN KEY(fkVer_id) REFERENCES wp_Veranstaltung(Ver_id), 
        PRIMARY KEY  (pers_id)
        )" . $charset_collate . ";";
        
        dbDelta($tablePers_Crea);
        
        $success = empty($wpdb->last_error);
        
        return $success;
    }
    
    if( $wpdb->get_var( "SHOW TABLES LIKE '$table_Sponsor'" ) != $table_Sponsor) {
        $tableSp_Crea = "CREATE TABLE " . $table_Sponsor . "(
        sp_id int(11) NOT NULL AUTO_INCREMENT, 
        Code varchar(255), 
        Name varchar(255), 
        PRIMARY KEY  (sp_id)
        )" . $charset_collate . ";";
        dbDelta( $tableSp_Crea );
        $success = empty($wpdb->last_error);
        
        return $success;
    }
    
    if( $wpdb->get_var( "SHOW TABLES LIKE '$table_SponKunde'" ) != $table_SponKunde ) {
        $tableSpKu_Crea = "CREATE TABLE " . $table_SponKunde . "(
        fkPers_id int(11), 
        fkSp_id int(11), 
        FOREIGN KEY(fkPers_id) REFERENCES wp_Personen(pers_id), 
        FOREIGN KEY(fkSp_id) REFERENCES wp_Sponsor(sp_id)
        )" . $charset_collate . ";";
        dbDelta( $tableSpKu_Crea );
        $success = empty($wpdb->last_error);
        
        return $success;
    }
}   
    register_activation_hook( __FILE__, 'artok_activate' );
Share Improve this question edited Jan 22, 2021 at 10:13 Artok Kotra asked Jan 21, 2021 at 11:24 Artok KotraArtok Kotra 112 bronze badges 1
  • I'm not familiar with the way this is structured: FOREIGN KEY(fkVer_id) REFERENCES wp_Veranstaltung(Ver_id), Try removing that line from the second table and see if that works and look through every line - if there's an error at any point it'll stop executing further, so start with the second table. – Tony Djukic Commented Jan 23, 2021 at 3:48
Add a comment  | 

1 Answer 1

Reset to default 0

Artok, your concatenation is incorrect... You forgot to continue the pattern with your $charset_collate variable.

If we look at $query1 we see this:

PRIMARY KEY  (ver_id)
) $charset_collate;";

What we should see is this:

PRIMARY KEY  (ver_id)
)" . $charset_collate . ";";

You should also avoid giving your functions generic names, I recognize you may have just changed it to activate() for the sake of not posting the actual name of your function here, but I see a lot of generic naming when I get work submitted to me and it creates conflicts.

function artok_activate(){
        global $wpdb;
        
        $charset_collate = $wpdb->get_charset_collate();
        
        require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
        
        //$verb = new mysqli('localhost','root','root','test4');
        $table_name1 = $wpdb->prefix . "Veranstaltung";
        $table_name2 = $wpdb->prefix . "Personen";
        $table_name3 = $wpdb->prefix . "Sponsor";
        $table_name4 = $wpdb->prefix . "sponkunde";
        
        //$wpdb->query("CREATE DATABASE eBagDB");
        $query1 = "CREATE TABLE " . $table_name1 . "(
        ver_id int(11) NOT NULL AUTO_INCREMENT, 
        VeranstaltungName varchar(255), 
        PRIMARY KEY  (ver_id)
        )" . $charset_collate . ";";
        $query2 = "CREATE TABLE " . $table_name2 . "(
        pers_id int(11) NOT NULL AUTO_INCREMENT, 
        Datum DATE, 
        Passwort varchar(255) UNIQUE, 
        Vorname varchar(255), 
        Nachname varchar(255), 
        Anrede varchar(255), 
        LoeschDatum DATE, 
        fkVer_id int(11), 
        FOREIGN KEY(fkVer_id) REFERENCES Veranstaltung(Ver_id), 
        PRIMARY KEY  (pers_id)
        )" . $charset_collate . ";";
        $query3 = "CREATE TABLE " . $table_name3 . "(
        sp_id int(11) NOT NULL AUTO_INCREMENT, 
        Code varchar(255), 
        Name varchar(255), 
        PRIMARY KEY  (sp_id)
        )" . $charset_collate . ";";
        $query4 = "CREATE TABLE " . $table_name4 . "(
        fkPers_id int(11), 
        fkSp_id int(11), 
        FOREIGN KEY(fkPers_id) REFERENCES Personen(pers_id), 
        FOREIGN KEY(fkSp_id) REFERENCES sponsor(sp_id)
        )" . $charset_collate . ";"; 
        
        /*$wpdb->query($query1);
        $wpdb->query($query2);
        $wpdb->query($query3);
        $wpdb->query($query4);*/
        
        //$ergCrDB = $wpdb->get_results($createDB, OBJECT);
        
        //$ergCrTbVer = $wpdb->get_results($createTbVer, OBJECT);
        /*$ergCrTbPers = $wpdb->get_results($createTbPers, OBJECT);
        $ergCrTbSp = $wpdb->get_results($createTbPersSp, OBJECT);
        $ergCrTbPersSp = $wpdb->get_results($createTbSponsor, OBJECT);*/
        
        dbDelta($query1);
        dbDelta($query2);
        dbDelta($query3);
        dbDelta($query4);
}
register_activation_hook(__FILE__,'artok_activate');

I hope this fixes the issue for you. :-)

Update:

When I tested adding one of the tables to a plugin I'm working on that also registers custom tables on activation, it worked.

Here's how the functions work for me:

function artok_db_create_tables() {
    global $wpdb
    require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
    $charset_collate        = $wpdb->get_charset_collate();
    $table_name1            = $wpdb->prefix . 'Veranstaltung';
    if( $wpdb->get_var( "SHOW TABLES LIKE '$table_name1'" ) != $table_name1 ) {
        $table1_sql = 'CREATE TABLE ' . $table_name1 . '(
        ver_id int(11) NOT NULL AUTO_INCREMENT, 
        VeranstaltungName varchar(255), 
        PRIMARY KEY  (ver_id)
        )' . $charset_collate . ';';
        dbDelta( $table1_sql );
    }//table_name1
}
register_activation_hook( __FILE__, 'artok_db_create_tables' );

I've only run it once for the first table, but you would then just repeat the pattern. I've added a condition that first checks if the table already exists before attempting to add it a second time.

发布评论

评论列表(0)

  1. 暂无评论