im试图在插件激活时创建一些表,但仅创建表2和4。我找不到我犯的错误,听说dbDelta非常棘手...
激活后,我可以在phpmyadmin中找到“ ceb_locations”和“ ceb_speakers”。我从从phpmyadmin下载的sql文件复制了SQL。有什么想法吗?
有人可以帮助我吗?
function create_plugin_database_tables()
{
global $table_prefix, $wpdb;
$tblname1 = 'ceb_events';
$wp_track_table1 = $table_prefix . "$tblname1 ";
$tblname2 = 'ceb_locations';
$wp_track_table2 = $table_prefix . "$tblname2 ";
$tblname3 = 'ceb_registrations';
$wp_track_table3 = $table_prefix . "$tblname3 ";
$tblname4 = 'ceb_speakers';
$wp_track_table4 = $table_prefix . "$tblname4 ";
#Check to see if the table exists already, if not, then create it
$sql = "";
if($wpdb->get_var( "show tables like '$wp_track_table1'" ) != $wp_track_table1) {
$sql .= " CREATE TABLE $tblname1 (";
$sql .= " `name` varchar(50) COLLATE latin1_german1_ci NOT NULL,";
$sql .= " `capacity` int(11) NOT NULL,";
$sql .= " `datetime` varchar(50) COLLATE latin1_german1_ci NOT NULL, ";
$sql .= " `description` varchar(1000) COLLATE latin1_german1_ci NOT NULL,";
$sql .= " `locationID` int(11) NOT NULL,";
$sql .= " `speakerIDS` varchar(100) COLLATE latin1_german1_ci NOT NULL,";
$sql .= " PRIMARY KEY (`id`)";
$sql .= " ) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_german1_ci;";
}
if($wpdb->get_var( "show tables like '$wp_track_table2'" ) != $wp_track_table2) {
$sql .= " CREATE TABLE $tblname2 (";
$sql .= " `id` int(11) NOT NULL AUTO_INCREMENT,";
$sql .= " `name` varchar(100) COLLATE latin1_german1_ci NOT NULL,";
$sql .= " `address` varchar(100) COLLATE latin1_german1_ci NOT NULL,";
$sql .= " `zip` varchar(100) COLLATE latin1_german1_ci NOT NULL,";
$sql .= " `city` varchar(100) COLLATE latin1_german1_ci NOT NULL,";
$sql .= " `country` varchar(100) COLLATE latin1_german1_ci NOT NULL,";
$sql .= " PRIMARY KEY (`id`)";
$sql .= " ) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_german1_ci;";
}
if($wpdb->get_var( "show tables like '$wp_track_table3'" ) != $wp_track_table3) {
$sql .= " CREATE TABLE $tblname3 (";
$sql .= " `id` int(11) NOT NULL AUTO_INCREMENT,";
$sql .= " company` varchar(100) COLLATE latin1_german1_ci NOT NULL,";
$sql .= " `name` varchar(100) COLLATE latin1_german1_ci NOT NULL,";
$sql .= " `email` varchar(100) COLLATE latin1_german1_ci NOT NULL,";
$sql .= " `address` varchar(100) COLLATE latin1_german1_ci NOT NULL,";
$sql .= " `zip` varchar(100) COLLATE latin1_german1_ci NOT NULL,";
$sql .= " `city` varchar(100) COLLATE latin1_german1_ci NOT NULL,";
$sql .= " `country` varchar(100) COLLATE latin1_german1_ci NOT NULL,";
$sql .= " `eventID` int(11) NOT NULL,";
$sql .= " `creation_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,";
$sql .= " PRIMARY KEY (`id`)";
$sql .= " ) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_german1_ci;";
}
if($wpdb->get_var( "show tables like '$wp_track_table4'" ) != $wp_track_table4)
{
$sql .= " CREATE TABLE $tblname4 (";
$sql .= " `id` int(11) NOT NULL AUTO_INCREMENT,";
$sql .= " `name` varchar(100) COLLATE latin1_german1_ci NOT NULL,";
$sql .= " `skills` varchar(200) COLLATE latin1_german1_ci NOT NULL,";
$sql .= " PRIMARY KEY (`id`)";
$sql .= " ) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_german1_ci;";
}
require_once( ABSPATH . '/wp-admin/includes/upgrade.php' );
dbDelta($sql);
}
register_activation_hook( __FILE__, 'create_plugin_database_tables' );