admin管理员组文章数量:1336293
I am using the following script to add a table to the wordpress database, but nothing is happening. I am not sure what I am doing wrong.
Any advice would be much appreciated.
function vw_postcode_checker_create_db ()
{
global $wpdb;
global $tableprefix;
// Create Table Name
$table_name =$tableprefix . "vw_postcode_checker";
// Write Query to add table to database
if ($wpdb->get_var('SHOW TABLES LIKE'.$table_name) !=$table_name)
{
$sql = "CREATE TABLE " . $table_name . " (
id INTEGER(10) UNSIGNED AUTO_INCREMENT,
postcode varchar(250) NOT NULL,
area varchar(250) NOT NULL
PRIMARY KEY (id)
)";
// Execute Query to Create Table
require_once (ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
add_option ('vw_postcode_checker_version', '0.1');
}
}
// Register table function
register_activation_hook (__FILE__,'vw_postcode_checker_create_db');
I am using the following script to add a table to the wordpress database, but nothing is happening. I am not sure what I am doing wrong.
Any advice would be much appreciated.
function vw_postcode_checker_create_db ()
{
global $wpdb;
global $tableprefix;
// Create Table Name
$table_name =$tableprefix . "vw_postcode_checker";
// Write Query to add table to database
if ($wpdb->get_var('SHOW TABLES LIKE'.$table_name) !=$table_name)
{
$sql = "CREATE TABLE " . $table_name . " (
id INTEGER(10) UNSIGNED AUTO_INCREMENT,
postcode varchar(250) NOT NULL,
area varchar(250) NOT NULL
PRIMARY KEY (id)
)";
// Execute Query to Create Table
require_once (ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
add_option ('vw_postcode_checker_version', '0.1');
}
}
// Register table function
register_activation_hook (__FILE__,'vw_postcode_checker_create_db');
Share
Improve this question
asked May 23, 2020 at 18:19
Victoria WhiteheadVictoria Whitehead
336 bronze badges
0
1 Answer
Reset to default 2Try:
function vw_postcode_checker_create_db () {
global $wpdb;
// Create Table Name
$table_name = $wpdb->prefix . "vw_postcode_checker";
// Write Query to add table to database
if ( $wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name) {
$sql = "CREATE TABLE $table_name (
id INTEGER(10) UNSIGNED AUTO_INCREMENT,
postcode varchar(250) DEFAULT '' NOT NULL,
area varchar(250) DEFAULT '' NOT NULL,
PRIMARY KEY (id)
);";
// Execute Query to Create Table
require_once (ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
add_option ('vw_postcode_checker_version', '0.1');
}
}
// Register table function
register_activation_hook (__FILE__,'vw_postcode_checker_create_db');
Note the use of $wpdb->prefix
and the closures after PRIMARY KEY (id)
https://codex.wordpress/Creating_Tables_with_Plugins
本文标签: customizationAdding a Table to the wordpress database
版权声明:本文标题:customization - Adding a Table to the wordpress database 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742406373a2468904.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论