我已经创建了一个插件,它将在数据库中创建一个关于插件激活的表。
当我启用插件并检查数据库时,没有要创建的新表。这是我的插件文件代码。
<?php
/*
Plugin Name: wp_course_management
Description: Wordpress plugin for course management for an education site.
Version: 1.0
Author: Ahmad Nawaz
Author URI: http://nawaz-ahmad.blogspot.com
*/
?>
<?php
global $jal_db_version;
$jal_db_version = "1.0";
function jal_install() {
global $wpdb;
global $jal_db_version;
$table_name = $wpdb->prefix . "classes";
$sql = $wpdb->prepare( "CREATE TABLE %s (
id mediumint(9) NOT NULL AUTO_INCREMENT,
time datetime DEFAULT \'0000-00-00 00:00:00\' NOT NULL,
name tinytext NOT NULL,
text text NOT NULL,
url VARCHAR(55) DEFAULT \'\' NOT NULL,
UNIQUE KEY id (id)
);", $table_name );
require_once(ABSPATH . \'wp-admin/includes/upgrade.php\');
dbDelta($sql);
add_option("jal_db_version", $jal_db_version);
}
function jal_install_data() {
global $wpdb;
$welcome_name = "Mr. Wordpress";
$welcome_text = "Congratulations, you just completed the installation!";
$rows_affected = $wpdb->insert( $table_name, array( \'time\' => current_time(\'mysql\'), \'name\' => $welcome_name, \'text\' => $welcome_text ) );
}
register_activation_hook(__FILE__,\'jal_install\');
register_activation_hook(__FILE__,\'jal_install_data\');
?>
我的代码中有什么问题。请帮忙。
最合适的回答,由SO网友:Ahmad 整理而成
而不是此代码
$sql = $wpdb->prepare( "CREATE TABLE %s (
id mediumint(9) NOT NULL AUTO_INCREMENT,
time datetime DEFAULT \'0000-00-00 00:00:00\' NOT NULL,
name tinytext NOT NULL,
text text NOT NULL,
url VARCHAR(55) DEFAULT \'\' NOT NULL,
UNIQUE KEY id (id)
);", $table_name );
我用过这个。
$sql ="CREATE TABLE ".$table_name." (
id mediumint(9) NOT NULL AUTO_INCREMENT,
time datetime DEFAULT \'0000-00-00 00:00:00\' NOT NULL,
name tinytext NOT NULL,
text text NOT NULL,
url VARCHAR(55) DEFAULT \'\' NOT NULL,
UNIQUE KEY id (id)
);";
它解决了我的问题。
SO网友:user25017
反应迟钝,但这可能有助于其他人理解why $wpdb->prepare should not be used with SQL CREATE statements.
答案是$wpdb->prepare用单引号包围字符串(%s)。从上述$wpdb->prepare生成的CREATE语句是无效的SQL:
CREATE TABLE \'wp_classes\' ( id mediumint(9) NOT NULL AUTO_INCREMENT,
time datetime DEFAULT \'0000-00-00 00:00:00\' NOT NULL,
name tinytext NOT NULL, text text NOT NULL,
url VARCHAR(55) DEFAULT \'\' NOT NULL, UNIQUE KEY id (id) );
而不是
CREATE TABLE wp_classes ( id mediumint...
SO网友:Arvind Pal
<?php
/*
Plugin Name: wp_course_management
Description: Wordpress plugin for course management for an education site.
Version: 1.0
Author: Ahmad Nawaz
Author URI: http://nawaz-ahmad.blogspot.com
*/
?>
<?php
global $jal_db_version;
$jal_db_version = "1.0";
function jal_install() {
global $wpdb;
global $jal_db_version;
$table_name = $wpdb->prefix . "classes";
$sql = "CREATE TABLE IF NOT EXISTS $table_name (
id mediumint(9) NOT NULL AUTO_INCREMENT,
time datetime DEFAULT \'0000-00-00 00:00:00\' NOT NULL,
name tinytext NOT NULL,
text text NOT NULL,
url VARCHAR(55) DEFAULT \'\' NOT NULL,
UNIQUE KEY id (id)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;";
$wpdb->query($sql);
}
function jal_install_data() {
global $wpdb;
$table_name = $wpdb->prefix . "classes";
$welcome_name = "Mr. Wordpress";
$welcome_text = "Congratulations, you just completed the installation!";
$rows_affected = $wpdb->insert( $table_name, array( \'time\' => current_time(\'mysql\'), \'name\' => $welcome_name, \'text\' => $welcome_text ) );
}
register_activation_hook(__FILE__,\'jal_install\');
register_activation_hook(__FILE__,\'jal_install_data\');
?>