gpt4 book ai didi

php - 无法在激活 wp 插件时创建多个自定义数据库表

转载 作者:可可西里 更新时间:2023-11-01 06:45:45 26 4
gpt4 key购买 nike

我无法在激活我的 wordpress 插件时在数据库中创建多个自定义表,它只创建最后一个表,就像这段代码中提到的“书签”而不是创建所有表(用户、字段、可见性、通知)等

function your_plugin_options_install() {
global $wpdb;
//global $your_db_name;
$uservar = $wpdb->prefix . 'user';
$fieldvar = $wpdb->prefix . 'field';
$visibilitytypevar = $wpdb->prefix . 'visibily';
$notificationvar = $wpdb->prefix . 'notification';
$jobtypevar = $wpdb->prefix . 'jobtype';
$bookmarkvar = $wpdb->prefix . 'bookmark';
// $profiledatavar = $wpdb->prefix . 'profiledata';
//$employeevar = $wpdb->prefix . 'employee';
//echo "some";
// create the ECPT metabox database table
//if($wpdb->get_var("SHOW TABLES LIKE '$yourtable'") != $yourtable)
//{
$sql = "CREATE TABLE IF NOT EXISTS " . $uservar . " (
`u_id` int(20) NOT NULL AUTO_INCREMENT,
`u_name` varchar(30) ,
`u_phonenumber` int(20) ,
`u_email` varchar(30) NOT NULL,
`u_password` varchar(50) NOT NULL,
PRIMARY KEY (u_id)
);";
$sql = "CREATE TABLE IF NOT EXISTS " . $fieldvar . " (
`f_id` int(20) NOT NULL AUTO_INCREMENT ,
`f_title` varchar(30) NOT NULL,
PRIMARY KEY (f_id)
);";

$sql = "CREATE TABLE IF NOT EXISTS " . $visibilitytypevar . " (
`v_id` int(20) NOT NULL AUTO_INCREMENT ,
`v_type` bool NOT NULL,
PRIMARY KEY (v_id)
);";

$sql = "CREATE TABLE IF NOT EXISTS " . $notificationvar . " (
`n_id` int(20) NOT NULL AUTO_INCREMENT ,
`u_id` int(20) NOT NULL,
PRIMARY KEY (n_id)
);";

$sql = "CREATE TABLE IF NOT EXISTS " . $jobtypevar . " (
`jt_id` int(20) NOT NULL AUTO_INCREMENT ,
`jt_title` varchar(30),
PRIMARY KEY (jt_id)
);";

$sql = "CREATE TABLE IF NOT EXISTS " . $bookmarkvar . " (
`bm_id` int(20) NOT NULL AUTO_INCREMENT ,
`u_id` int(20) NOT NULL,
`bm_title` varchar(30),
PRIMARY KEY (bm_id)
);";


require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
}

//}
// run the install scripts upon plugin activation
register_activation_hook(__FILE__,'your_plugin_options_install');

最佳答案

您正在覆盖您的 $sql 变量。只需在每次查询后使用 dbDelta 函数。像下面的格式。

require_once(ABSPATH . 'wp-admin/includes/upgrade.php');

// your first query
$sql = '...';
dbDelta($sql);

// your second query
$sql = '...';
dbDelta($sql);

.
.
.


// your nth query
$sql = '...';
dbDelta($sql);

关于php - 无法在激活 wp 插件时创建多个自定义数据库表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29889943/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com