Creating table with plugin is not working

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

2 Answers
2

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

Leave a Comment