0

This is the code below I am using to create a table when installing plugin. But, I tried many ways and for some reason it is not working. Is there any other efficient way of doing this? Please Help, thanks in Advanced

global $wpdb;
function creating_order_tables() {
    global $wpdb;

    $ptbd_table_name = $wpdb->prefix . 'printtextbd_order';

    if ($wpdb->get_var("SHOW TABLES LIKE '". $ptbd_table_name ."'"  ) != $ptbd_table_name ) {

        $sql  = 'CREATE TABLE exone(
                customer_id INT(20) AUTO_INCREMENT,
                customer_name VARCHAR(255),
                order_type VARCHAR(255),
                choosen_template VARCHAR(255),
                order_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
                PRIMARY KEY(customer_id))';

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


register_activation_hook(__FILE__, 'creating_order_tables');
  • please edit the question, define "not working": and add whatever relevant information from your debugging attempts – Mark Kaplun Dec 12 '16 at 14:23
1

Try using a different method. WordPress fires a hook during plugin activation activate_yourplugin/filename.php. You can use this to create tables.

function creating_order_tables() {

    if(!get_option('tables_created', false)) {

        global $wpdb;

        $ptbd_table_name = $wpdb->prefix . 'printtextbd_order';

        if ($wpdb->get_var("SHOW TABLES LIKE '". $ptbd_table_name ."'"  ) != $ptbd_table_name ) {

            $sql  = 'CREATE TABLE exone(
            customer_id INT(20) AUTO_INCREMENT,
            customer_name VARCHAR(255),
            order_type VARCHAR(255),
            choosen_template VARCHAR(255),
            order_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
            PRIMARY KEY(customer_id))';

            if(!function_exists('dbDelta')) {
                require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
            }

            dbDelta($sql);
            echo "Tables created...";
            update_option('tables_created', true);
        }
    }
}
add_action('activate_yourplugin/yourplugin.php', 'creating_order_tables');
|improve this answer|||||
1

Try this one, Also remember int can have maximum of int(11)

<?php
global $wpdb;

// Set table name
$table = $wpdb->prefix . 'exone';

$charset_collate = $wpdb->get_charset_collate();

// Write creating query
$query =  "CREATE TABLE IF NOT EXISTS  ".$table." (
            customer_id INT(11) AUTO_INCREMENT,
            customer_name VARCHAR(255),
            order_type VARCHAR(255),
            choosen_template VARCHAR(255),
            order_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
            PRIMARY KEY(customer_id)
            )$charset_collate;";

// Execute the query
$wpdb->query( $query );
|improve this answer|||||

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.