1

After reading many question and answers and many had asked the same question I have been following the same instruction as the people instructed but my bad it's not working I have used

  • dbDelta($sql)
  • $wpdb->query($sql)

It's not working

sample code

<?php
/*
Plugin Name: ContactUs
Plugin URI: 
Description: Simple Plugin Developed for Testing purpose
Version: 1.0.0
Author: rajeshw
Author URI: codelisense.com
*/  

//Creating table
    function contact_installation(){
        global $wpdb;
        $table_name = $wpdb->prefix. "contactus";

        if($wpdb->get_var ('SHOW TABLES LIKE' .$table_name ) != $table_name){
            $sql= "CREATE TABLE" .$table_name. "(
                   id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, 
                   firstname VARCHAR(30) NOT NULL,
                   lastname VARCHAR(30) NOT NULL,
                   email VARCHAR(50),
                   reg_date TIMESTAMP
                   )";
            require_once (ABSPATH. 'wp-admin/includes/upgrade.php' );
            $wpdb->query($sql);

            //had replaced the above line with this line
            dbDelta($sql);
        }
    }
    register_activation_hook(__FILE__, 'contact_installation');

Table is not been created... Please help

4

Your dot and quote notation is funky. Try this:

if($wpdb->get_var("SHOW TABLES LIKE '$table_name'" ) != $table_name){
    $sql= "CREATE TABLE $table_name (
           id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, 
           firstname VARCHAR(30) NOT NULL,
           lastname VARCHAR(30) NOT NULL,
           email VARCHAR(50),
           reg_date TIMESTAMP
           );";
    require_once (ABSPATH. 'wp-admin/includes/upgrade.php' );
    dbDelta($sql);
}
2

Try this code.

Create a file like "my_db.php" and paste this code:

global $table_version;
$table_version = "1.0";

function func_table_contactus(){
    creat_table_contactus();
}

function creat_table_contactus(){
    global $wpdb;
    global $table_version;

    $table_contactus = $wpdb->prefix."contactus";
    $slider_ver = get_option("table_version");

    $check_table_contactus = $wpdb->get_var("SHOW TABLES LIKE '$table_contactus'");

    if($check_table_contactus != $table_contactus ||  $slider_ver != $table_version ) {
         $sql= "CREATE TABLE" .$table_name. "(
                   id INT(6) UNSIGNED NOT NULL AUTO_INCREMENT, 
                   firstname VARCHAR(30) NOT NULL,
                   lastname VARCHAR(30) NOT NULL,
                   email VARCHAR(50),
                   reg_date TIMESTAMP
           PRIMARY KEY (id)
               )";

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

    update_option("table_version", $table_version);
    }
    else{
        add_option("table_version", $table_version);
    }

}

and in your plugin main file

<?php
/*
Plugin Name: ContactUs
Plugin URI: 
Description: Simple Plugin Developed for Testing purpose
Version: 1.0.0
Author: rajeshw
Author URI: codelisense.com
*/  

require "my_db.php";
    add_action('plugins_loaded', 'func_table_contactus');

Hope you find your solution

  • OOh..!! this too works.. Thank you so much for you reply... – lost_in_magento Dec 3 '14 at 13:01

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.