嗨,我想知道我可以创建两个tables
安装自定义插件时。这就是我创建tables
在数据库中
function wnm_install(){
global $wpdb;
global $wnm_db_version;
$sms_table = $wpdb->prefix . "smsfactory";
if($wpdb->get_var("show tables like \'". $sms_table . "\'") != $sms_table){
$sql_sms_table = "CREATE TABLE ". $sms_table . " (
SfID int(11) NOT NULL AUTO_INCREMENT,
sf_name varchar(128) NOT NULL,
start_duration date NOT NULL,
end_duration date NOT NULL,
activity varchar(500) NOT NULL,
survey_settings varchar(50) NOT NULL,
`limit` varchar(50) NOT NULL,
goal varchar(100) DEFAULT NULL,
PRIMARY KEY (SfID)
) ";
}
$sms_message_table = $wpdb->prefix . "smsfactorymessagetemplate";
if($wpdb->get_var("show tables like \'". $sms_message_table . "\'") != $sms_message_table){
$sql = "CREATE TABLE ". $sms_message_table . " (
sfID int(11) NOT NULL AUTO_INCREMENT,
sftemplate_name varchar(256) NOT NULL,
sftemplate_type varchar(128) NOT NULL,
PRIMARY KEY (sfID)
) ";
}
require_once(ABSPATH . \'wp-admin/includes/upgrade.php\');
dbDelta($sql);
add_option("wnm_db_version", $wnm_db_version);
}
现在这段代码的问题是它创建了第二个表,跳过了第一个表,所以请告诉我有什么解决方法
SO网友:Adam
在原始代码中,您只调用dbDelta($sql)
而不是dbDelta($sql_sms_table)
而且
更好的方法
dbDelta()
可以接受查询数组,因此我们需要在
$sql
变量来存储条件求值时的查询
true
.
function wnm_install() {
global $wpdb, $wnm_db_version;
$sql = array();
//sms table
$sms_table = $wpdb->prefix . "smsfactory";
if( $wpdb->get_var("show tables like \'". $sms_table . "\'") !== $sms_table ) {
$sql[] = "CREATE TABLE ". $sms_table . " (
SfID int(11) NOT NULL AUTO_INCREMENT,
sf_name varchar(128) NOT NULL,
start_duration date NOT NULL,
end_duration date NOT NULL,
activity varchar(500) NOT NULL,
survey_settings varchar(50) NOT NULL,
`limit` varchar(50) NOT NULL,
goal varchar(100) DEFAULT NULL,
PRIMARY KEY (SfID)
) ";
}
//sms messages table
$sms_message_table = $wpdb->prefix . "smsfactorymessagetemplate";
if( $wpdb->get_var("show tables like \'". $sms_message_table . "\'") !== $sms_message_table ) {
$sql[] = "CREATE TABLE ". $sms_message_table . " (
sfID int(11) NOT NULL AUTO_INCREMENT,
sftemplate_name varchar(256) NOT NULL,
sftemplate_type varchar(128) NOT NULL,
PRIMARY KEY (sfID)
) ";
}
if ( !empty($sql) ) {
require_once(ABSPATH . \'wp-admin/includes/upgrade.php\');
dbDelta($sql);
add_option("wnm_db_version", $wnm_db_version);
}
}