php - 无法在 wordpress 中创建表格
问题描述
我无法在 wordpress 中创建表格,我不明白为什么?
private function create($prefix)
{
$sql = "CREATE TABLE " . $prefix . "submit (
submit_id bigint PRIMARY KEY AUTO_INCREMENT,
post_id bigint,
user_id bigint,
author text,
user_email text,
source text,
pass text,
language text,
time datetime,
CONSTRAINT post_id FOREIGN KEY (post_id) REFERENCES wp_posts(ID),
CONSTRAINT user_id FOREIGN KEY (user_id) REFERENCES wp_users(ID)
)";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
var_dump(dbDelta($sql));
}
节目var_dump()
:
array(1) { ["wp_submit"]=> string(23) "创建表 wp_submit" }
解决方案
add_action("after_switch_theme", "my_custom_table_setup");
function my_custom_table_setup()
{
global $wpdb;
$create_table_query = "
CREATE TABLE {$wpdb->prefix}products_by_sku (
id int(11) NOT NULL auto_increment,
product_id int(11) NOT NULL,
lot_id varchar(25) NOT NULL,
title varchar(255) NOT NULL,
LP varchar(25) NOT NULL,
catgeory varchar(255) NOT NULL,
product_name varchar(255) NOT NULL,
UPC varchar(255) DEFAULT NULL,
ASIN varchar(255) DEFAULT NULL,
original_retail_price float DEFAULT NULL,
quantity int(11) DEFAULT NULL,
total_original_retail float NOT NULL DEFAULT 0,
stock_image text,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
";
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
dbDelta( $create_table_query );
}
试试这个代码。
谢谢你。
推荐阅读
- sql - MS Access 查询,如何创建仅显示文件更改位置的最近日期的文件库存查询
- html - 将登录面板移动到屏幕中心
- javascript - Promise 链接到可能已解决的 Promise
- testing - 是否可以让 GitHub 自述文件图像跟随重定向?
- visual-studio-code - vscode devcontainer定义中的自定义键绑定
- reactjs - 如何通过 D3 从 ReactJS 中删除之前的实例?
- c#-4.0 - 与 CMQC.MQSO_SET_CORREL_ID 等效的 IBM MQ XMS 选项是什么
- c++ - OpenGL:有没有办法只保留源和目标之间的最大值?
- facebook - 如何为非 App-Scoped User ID 建立永久且公开的 Facebook 个人资料照片 URL?
- visual-studio - VS2019 中的 Xamarin Forms Designer 其他外形尺寸