0

As my question is duplicate, I am still stuck in my code, and no solution has been found.

I create a function in the plugin to create a table after activating the plugin. But the table is not created after activating the plugin. I have placed this code in the main file of my plugin.

Please overlook my code :

register_activation_hook(__FILE__,'like_dislike_counter_install');

function like_dislike_counter_install() {
    global $wpdb;

    $table_name = $wpdb->prefix . 'subpostvotingcount';
    $charset_collate = $wpdb->get_charset_collate();

    $sql = "CREATE TABLE $table_name (
          sno int(10) NOT NULL AUTO_INCREMENT,
          postid int(50) NOT NULL,
          subpostid varchar(5) NOT NULL,
          votingcount varchar(50) NOT NULL,
          postclicktime timestamp(5) NOT NULL DEFAULT CURRENT_TIMESTAMP
        )  $charset_collate;";

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

1 Answer 1

0

You create auto column sno int(10) NOT NULL AUTO_INCREMENT, and forget to define it as key, add PRIMARY KEY (sno) to your sql.

$sql = "CREATE TABLE $table_name (
      sno int(10) NOT NULL AUTO_INCREMENT,
      postid int(50) NOT NULL,
      subpostid varchar(5) NOT NULL,
      votingcount varchar(50) NOT NULL,
      postclicktime timestamp(5) NOT NULL DEFAULT CURRENT_TIMESTAMP,
      PRIMARY KEY (sno)
    )  $charset_collate;";

If still won't work, try replace postclicktime timestamp(5) with postclicktime timestamp. Before MySQL 5.6, MySQL did not support fractional seconds stored as part of a TIMESTAMP datatype.

10
  • I added PRIMARY KEY (sno) and replaced postclicktime timestamp, But still table is not creating. Shall i again upload the plugin and activate it? Commented Sep 3, 2018 at 11:52
  • Update plugin (just the changed file), deactivate it and then activate. @PuspalataPanigrahi
    – nmr
    Commented Sep 3, 2018 at 11:56
  • I removed the plugin and again uploaded & activate it, But still the table not created in my Database. Commented Sep 3, 2018 at 11:59
  • Any error message? Do you have debug enabled? If not then add define('WP_DEBUG', TRUE); define('WP_DEBUG_LOG', TRUE); to wp-config.php, again try to activate the plugin and check error in log file (wp-content/debug.log). @PuspalataPanigrahi
    – nmr
    Commented Sep 3, 2018 at 12:16
  • 1
    Yes it was a sql problem. Actually i forgot to add comma in the below line : postclicktime timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP Commented Sep 3, 2018 at 13:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.