我希望有人能帮助我。
我尝试执行此代码来安装插件的表。但由于某种原因,它不起作用(没有出现在我的wordpress数据库中),我也不知道为什么。有人能帮我吗?
<?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
到目前为止,这个问题已部分解决。在我的wp配置中,选择了错误的数据库。感谢您迄今为止的帮助!:)
我还有一个问题:现在表创建工作正常,但只创建了第一个表。其他三个没有。有人知道为什么吗?
我的代码现在如下所示:
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\' );
SO网友:Tony Djukic
Artok,你的连接不正确。。。您忘记使用$charset\\u collate变量继续该模式。
如果我们看$query1
我们看到:
PRIMARY KEY (ver_id)
) $charset_collate;";
我们应该看到:
PRIMARY KEY (ver_id)
)" . $charset_collate . ";";
您还应该避免给函数起泛型名称,我知道您可能刚刚将其更改为
activate()
为了不在这里发布函数的实际名称,但是当我收到提交给我的工作时,我看到了很多通用名称,这会产生冲突。
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\');
我希望这能为您解决问题。:-)
Update:
当我测试将其中一个表添加到我正在处理的插件中,该插件也会在激活时注册自定义表时,它起到了作用。
以下是这些函数对我的作用:
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\' );
我只对第一个表运行了一次,但您只需重复该模式即可。我添加了一个条件,首先检查表是否已经存在,然后再尝试添加第二次。