我的自定义插件没有在数据库中创建数据库表

时间:2012-02-08 作者:Ahmad

我已经创建了一个插件,它将在数据库中创建一个关于插件激活的表。

当我启用插件并检查数据库时,没有要创建的新表。这是我的插件文件代码。

<?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\');

?>
我的代码中有什么问题。请帮忙。

3 个回复
最合适的回答,由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\');

?>
结束

相关推荐

Where are plugins located

我通常通过FTP下载和安装插件,但我已经从web界面安装了一些插件。我正在尝试将WP从远程移动到本地,我得到一个空白的WP管理屏幕。我认为是我的一个插件导致了这个问题。我已将wp-content/plugins重命名为wp-content/u-plugins,以禁用该文件夹中的所有插件。但我安装的插件没有出现在插件文件夹中,我认为这是导致wp管理屏幕空白的原因。请告知。谢谢