日安,请我试图在插件激活创建表。有些表创建成功,为什么有些不。一个问题是支付服务名称和货币部分。它创建了表列,但没有行与默认值。我已经尝试了所有我可以,但行仍然没有显示。我甚至删除了表,并再次激活插件重新创建,但仍然没有表行内的表携带指定的默认值。请问出路是什么??
function hdm_plugin_activate() {
global $wpdb;
// Create a custom table to store keywords and their status
$table_name_keywords = $wpdb->prefix . 'hdm_article_keywords';
$charset_collate = $wpdb->get_charset_collate();
$sql_keywords = "CREATE TABLE $table_name_keywords (
id mediumint(9) NOT NULL AUTO_INCREMENT,
keyword varchar(255) NOT NULL,
status varchar(20) NOT NULL,
selected_by varchar(255) DEFAULT NULL,
assigned_by mediumint(9) DEFAULT NULL,
assigned_time datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
hours_to_complete int(11) DEFAULT 0 NOT NULL,
PRIMARY KEY (id)
) $charset_collate;";
// Create the table for cost per word
$table_name_cost_per_word = $wpdb->prefix . 'hdm_cost_per_word';
$sql_cost_per_word = "CREATE TABLE $table_name_cost_per_word (
id mediumint(9) NOT NULL AUTO_INCREMENT,
cost_per_word decimal(10, 3) NOT NULL,
PRIMARY KEY (id)
) $charset_collate;";
// Create the table for currency
$table_name_currency = $wpdb->prefix . 'hdm_article_keyword_currency';
$sql_currency = "CREATE TABLE $table_name_currency (
id mediumint(9) NOT NULL DEFAULT '1',
currency_code varchar(3) NOT NULL DEFAULT 'USD',
PRIMARY KEY (id)
) $charset_collate;";
// Create the table for payment service names
$table_name_payments = $wpdb->prefix . 'hdm_keyword_payments';
$sql_payments = "CREATE TABLE $table_name_payments (
id mediumint(9) NOT NULL DEFAULT '1',
payment1 VARCHAR(255) NOT NULL DEFAULT 'Paypal ID',
payment2 VARCHAR(255) NOT NULL DEFAULT 'Payoneer Email',
PRIMARY KEY (id)
) $charset_collate;";
// Add 'Content Writer' role
add_role('hdm_content_writer_role', __('HDM Content Writer'), array(
'read' => true,
'edit_posts' => true,
'delete_posts' => true,
'upload_files' => true, // Allow adding images to posts
// Add other capabilities as needed
));
// Add 'HDM Editor' role
add_role('hdm_proofreader', __('HDM Editor'), array(
'read' => true,
'edit_posts' => true,
'delete_posts' => true,
'upload_files' => true,
'edit_published_posts' => true,
'publish_posts' => true,
'moderate_comments' => true,
// Add other capabilities as needed
));
// Use dbDelta to create/modify tables
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql_keywords);
dbDelta($sql_cost_per_word);
dbDelta($sql_currency);
$wpdb->query($sql_payments);
}
字符串
我尝试使用下面的代码来强制创建。它工作,但由于ID值1再次激活时返回致命错误。当我删除ID值时,它工作,但在每次激活和停用后继续创建新行。
// Insert default currency options for rows not created
$default_currency_code = 'USD';
$wpdb->insert($table_name_currency, array('currency_code' => $default_currency_code));
$wpdb->insert($table_name_payments, array('payment1' => 'Paypal ID', 'payment2' => 'Payoneer Email'));
型
1条答案
按热度按时间rqmkfv5c1#
先不考虑这是一个Wordpress安装的事实,你所做的并没有多大意义。你所做的核心是在你引用的两个“有问题”的表中创建一个
PRIMARY KEY
字段(也就是说,一个与该表中的所有其他行相比确保唯一的字段),该字段具有DEFAULT
值。你怎么能期望它在逻辑上可能有一个唯一的
PRIMARY KEY
,它只是默认为一个静态值,特别是当INSERT
处理数据时,你没有显式地分配一个id
值?虽然假设这 * 可能 * 工作(以及为什么这不是一个错误的原因)如果您有一些算法来生成有效id
值,并在INSERT
语句中显式地提供它们,那么这似乎不是您想要的。解决方案是删除
id
列的DEFAULT
,并允许MySQL的AUTO_INCREMENT
功能为您处理分配:字符串