0

I opened a new question, just to be sure. The former request has partly been resolved. I got another question regarding creating tables for wordpress: Only one table is created (The first one), but the others 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' );
5
  • Probably your code finishes too early because of the return $success; in each of the ifs. – kero Jan 22 at 15:43
  • Ok, I deleted these returns. Now two tables are created, The first and the third one. The other two still not :( Is there something with the foreign key? – Artok Kotra Jan 22 at 16:05
  • 1
    you should be using dbDelta to create tables, it'll even upgrade the schema of existing tables if you adjust the columns – Tom J Nowell Jan 22 at 16:08
  • Ok, I am using dbdelta now. But still only the first and third table are created. The second and fourth one, those with the foreign keys, still not. – Artok Kotra Jan 22 at 16:14
  • I found out, that dbdelta() is not recommended because it doesn't support foreign keys. So I changed it again. I also found out, that after creating the second table manually, the fourth table is created automatically as well. But I still don't know, why the second table is not created automatically. – Artok Kotra Jan 22 at 17:51

Your Answer

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

Browse other questions tagged or ask your own question.