0

I have created a plugin which will create a table in database on plugin activation.

When I enabled the plugin and check the database, there was no new table which I want to create. Here is my plugin file code.

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

?>

What is the problem in my code. Please help.

  • make sure you are activating the plugin as a Wp administrator. Also, does it output activation successful message. As the code it a copy of one in the codex, it should work. There seems no problem in it. – booota Feb 8 '12 at 7:49
  • I have removed the $wpdb->prepare function and it now works. – Ahmad Feb 8 '12 at 7:54
  • try creating the table in mysql using the create command used in $wpdv->prepare function. – booota Feb 8 '12 at 8:49
  • 1
    @Ahmad please post the resolution as an answer for the benefit of the community. You can close accept as the right answer within two days. – Brian Fegter Feb 8 '12 at 15:51
2

Late response, but this might help others to understand why $wpdb->prepare should not be used with SQL CREATE statements.

The answer is that $wpdb->prepare surrounds strings (%s) with single quote marks. The CREATE statement produced from the above $wpdb->prepare is invalid 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) );

instead of

CREATE TABLE wp_classes ( id mediumint...
1

Instead of this code

$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 );

I used this.

$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)
    );";

and it solved my problem.

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

?>
  • With no explanation as to what you're doing or why, this is not an answer. – EAMann Feb 8 '12 at 16:35
  • Here i have shown code to add table while activate of plugin – Arvind Pal Feb 9 '12 at 7:15
  • You shouldn't expect people to read through every line to code in the question and your answer just to figure out what small part you changed. You should add an introduction to your answer explaining what the problem was and what you changed to fix it. – Ian Dunn Feb 11 '12 at 20:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.