0

Primarily this is the only code in my main plugin page. It should create table in the database while I am activating it. But, it is not working at all. Plugin activating successfully. Please, help me to figure this out.

/*
* Plugin Name: Titan Video Plugin
* Description: A simple and my first plugin, just for testing.
* Author: Allen Titan.
* Version: 0.1.
* License: GPL3
*/

if(!defined('ABSPATH')) exit;

register_activation_hook( __FILE__, 'titan_vedio_table' );

function titan_vedio_table() {
global $wpdb;
global $titan_db_version;

$table_name = $wpdb->prefix ."titanvedio";

$sql = "CREATE TABLE " . $table_name . "(
    id BIGINT(25) NOT NULL AUTO_INCREMENT,
    time datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
    videoname VARCHAR(255) NOT NULL,
    video-des VARCHAR(255) NOT NULL,
    UNIQUE KEY id (id)
);";

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

add_option( 'titan_db_version', $titan_db_version );
}
  • where can i buy a worpdress? – majick Apr 3 '16 at 7:17
0

SQL is fairly temperamental syntax, not sure if it will make a difference but try adding the missing space before ( and removing ; from the end:

$sql = "CREATE TABLE " . $table_name . " (
    id BIGINT(25) NOT NULL AUTO_INCREMENT,
    time datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
    videoname VARCHAR(255) NOT NULL,
    video-des VARCHAR(255) NOT NULL,
    UNIQUE KEY id (id)
)";

The other thing I wonder if hyphens are allowed ie. video-des, since most table names use _ it made me think... this answer seems to suggest is it fine only if it is enclosed with the correct quotes ie. backticks not single quotes - so you may want to change it anyway to avoid future problems:

https://stackoverflow.com/questions/3168644/can-a-table-field-contain-a-hyphen

(although it is referring to table names not column key names so not totally sure on this.)

In any case you can check the return value of dbDelta it may give you a better idea. Since you can't probably see the result during activation due to redirects, I'd write it to a file:

$result = dbDelta($sql);
ob_start(); print_r($result); 
file_put_contents(get_template_directory().'/dbdebug.txt',ob_get_contents());
ob_end_clean();
  • I figured it out. The hyphen was the problem there. And It has worked fine. Thanks @majick for your help. – Allen Titan Apr 3 '16 at 7:33
  • sweet so it was that after all, glad it worked out – majick Apr 3 '16 at 7:49

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.