0

As my question is duplicate, still i am stuff in my code, no solution found.

I create a function in the plugin to create a table after activating the plugin. But the table is not creating after activate the plugin.I have placed this code in 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 );        
}
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.

  • I added PRIMARY KEY (sno) and replaced postclicktime timestamp, But still table is not creating. Shall i again upload the plugin and activate it? – Puspalata Panigrahi Sep 3 '18 at 11:52
  • Update plugin (just the changed file), deactivate it and then activate. @PuspalataPanigrahi – nmr Sep 3 '18 at 11:56
  • I removed the plugin and again uploaded & activate it, But still the table not created in my Database. – Puspalata Panigrahi Sep 3 '18 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 Sep 3 '18 at 12:16
  • Below error is showing : The plugin generated 386 characters of unexpected output during activation. If you notice “headers already sent” messages, problems with syndication feeds or other issues, try deactivating or removing this plugin. – Puspalata Panigrahi Sep 3 '18 at 12:23

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.