0

This is the php page through which i created the table , now i want to update the table. Not not able to do.

<?php
/**
 * @package wplab-campaign
 * @version 1.0
 */
/*
Plugin Name: Wplab Campaign

Description: Sinple Pluign to host capaign
Author: Saurabh Ranjan
Version: 1.0

*/


function my_plugin_create_db() {

    global $wpdb;
    $version = get_option( 'my_plugin_version', '1.0' );
    $charset_collate = $wpdb->get_charset_collate();
    $table_name = $wpdb->prefix . 'my_analysis';

    $sql = "CREATE TABLE $table_name (
        id mediumint(9) NOT NULL AUTO_INCREMENT,
        time datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
        views smallint(5) NOT NULL,
        clicks smallint(5) NOT NULL,
        UNIQUE KEY id (id)
    ) $charset_collate;";

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

    if ( version_compare( $version, '2.0' ) < 0 ) {
        $sql = "CREATE TABLE $table_name (
          id mediumint(9) NOT NULL AUTO_INCREMENT,
          time datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
          views smallint(5) NOT NULL,
          clicks smallint(5) NOT NULL,
          blog_id smallint(5) NOT NULL,
          UNIQUE KEY id (id)
        ) $charset_collate;";
        dbDelta( $sql );

        update_option( 'my_plugin_version', '2.0' );

    }


}




function my_plugin_insert_db() {
   global $wpdb;
  $table_name = $wpdb->prefix . 'my_analysis';
   $views = "1";
   $clicks = "1";
    $blog_id = "123s";

   $rows_affected = $wpdb->insert( $table_name, array( 'time' => current_time('mysql'), 'views' => $views, 'clicks' => $clicks, 'blog_id' =>$blog_id  ) );
}




function my_activation_func() {
    file_put_contents( __DIR__ . '/wplab-campaign-log.txt', ob_get_contents() );
}


register_activation_hook( __FILE__, 'my_activation_func' );
register_activation_hook( __FILE__, 'my_plugin_create_db' );
register_activation_hook( __FILE__, 'my_plugin_insert_db' );

missing coloumn

Note: After saving the above page , I disabled the plugin and activated the plugin to see the changes , but nothing change even got below warning

The plugin generated 585 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.

0

WordPress recommendations for the register_activation_hook hook are:

  1. Don't do echo directly in the register_activation_hook callback function. Do it other hooks like init or admin_init
  2. Make sure you are closing the PHP tag ?>. Otherwise, you will get the Header already sent notifications.

I have tested your code, I think you are closing the PHP tag ?> at the end of the file.

  • As you can see i am not using echo in my code and i checked my file i am not using ? > – inrsaurabh Aug 3 '17 at 15:05
  • Which version of WP are you using? Also, change hook of my_activation_func function into admin-init. – Syed Fakhar Abbas Aug 3 '17 at 21:02

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.