尝试创建新的Wordpress数据库表 [英] Trying To Create New Wordpress Database Table
本文介绍了尝试创建新的Wordpress数据库表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
只需尝试在插件激活时创建新的数据库表即可。
为了热爱生活,我无法弄清为什么这行不通。
Just trying to create a new database table on plugin activation. For the love of life I cannot figure out why this will not work.
function super_simple_photo_activate() {
global $wpdb;
$table_name = $wpdb->prefix."super_simple_photo_options";
if ($wpdb->get_var('SHOW TABLES LIKE '.$table_name) != $table_name) {
$sql = 'CREATE TABLE '.$table_name.'(
thumbs_max VARCHAR(3),
image_max VARCHAR(4),
image_quality VARCHAR(3),
PRIMARY KEY (id))';
require_once(ABSPATH.'wp-admin/includes/upgrade.php');
dbDelta($sql);
add_option("super_simple_photo_db_version", "1.0");
}
}
register_activation_hook(__FILE__, 'super_simple_photo_activate');
我花了至少5个小时对此进行了修补,但是没有运气,激活也没有错误
I've spent at least 5 hours tinkering with this but with no luck, no error either on activation.
诀窍是什么-id整数不为空-谢谢t.thielemans
What did the trick - id INTEGER NOT NULL - thanks t.thielemans
$sql = 'CREATE TABLE '.$table_name.'(
id INTEGER NOT NULL,
thumbs_max VARCHAR(3),
image_max VARCHAR(4),
image_quality VARCHAR(3),
PRIMARY KEY (id))';
推荐答案
您的创建表语法错误,应为:
Your create table syntax is wrong, should be:
$sql = 'CREAT TABLE '.$table_name.'(
-----
$sql = 'CREATE TABLE '.$table_name.'(
编辑:定义主键
$sql = 'CREATE TABLE '.$table_name.'(
id INTEGER NOT NULL,
thumbs_max VARCHAR(3),
image_max VARCHAR(4),
image_quality VARCHAR(3),
PRIMARY KEY (id))';
来自W3schools的SQL的其他信息: http://www.w3schools.com/sql/sql_primarykey.asp
Bit of extra info on SQL from W3schools: http://www.w3schools.com/sql/sql_primarykey.asp
这篇关于尝试创建新的Wordpress数据库表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文