0

I'm trying to learn how to create and use custom tables on wpdb for a plugin that I'm going to build. So far I've seen a bunch of tutorials and know how to create a table but I'm facing problems storing data and debugging my table.

so for my plugin I've created a main class where these functions are called

public function pre_install(){
    global $wpdb;
    global $pre_db_version;
    $pre_db_version='1.1';

    $table_name = $wpdb->prefix . 'prelauncher';

    $charset_collate = $wpdb->get_charset_collate();

    $sql = "CREATE TABLE $table_name (
    id mediumint(9) NOT NULL AUTO_INCREMENT,
    username tinytext NOT NULL,
    email varchar(255) NOT NULL,
    UNIQUE KEY id (id)
    ) $charset_collate;";

require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
        dbDelta( $sql );

add_option( 'pre_db_version', $pre_db_version );

}  

and then outside the class I called

register_activation_hook( __FILE__, array('main_class','pre_install'));

This should create a database table called wp_prelauncher with two columns, username and email which it does but it also creates two extra columns called my_title and show_title . Where are these two columns coming from I have no idea. I have searched my entire code but found nothing.

Also if anyone could point me in the direction where I could learn how to save user input in this table it will be a big help.

0

I guess, you can use the following function

public function pre_install()   {
global $wpdb;
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
$sql="
   CREATE TABLE IF NOT EXISTS `".$wpdb->prefix."prelauncher`
   (
      id mediumint(9) NOT NULL auto_increment,
      username tinytext NOT NULL,
      email varchar(255) NOT NULL,
      PRIMARY KEY  (`main_id`)
 );";
dbDelta($sql);
}

and than your activation hook.

register_activation_hook( __FILE__, array('main_class','pre_install'));

I dont think the $pre_db_versions - required for the this table creation.

I hope this will solve your problem.

  • Thank you for answering. I actually found that while previously I called the two extra columns, they were still showing up even after I erased them from code. I had to add a deactivation hook for my plugin so it can drop the previously created table on deactivation. After dropping the table and reactivating presto they were no longer there. – AndyWanna_KnowItAll Nov 11 '15 at 13:44
  • welcome. hope your trouble solved – Kvvaradha Nov 13 '15 at 5:02

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.