创建自定义模块时无法更新数据库

时间:2017-08-02 作者:inrsaurabh

这是我创建表的php页面,现在我想更新表。无法做到。

<?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: 保存了上面的页面后,我禁用了插件并激活了插件以查看更改,但没有任何更改低于警告

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.

1 个回复
SO网友:Syed Fakhar Abbas

WordPress建议register_activation_hook 挂钩包括:

不要这样做echo 直接在register_activation_hook 回调函数。做其他钩子initadmin_init?>. 否则,您将获得Header already sent notifications.我已经测试了您的代码,我认为您正在关闭PHP标记?> 在文件末尾。

结束

相关推荐

get_terms return errors

嗨,当我尝试get_terms(); 通过此代码在主题选项中$catalogs_terms = get_terms( \'catalogs\' ); $mycatalogs = array( -1 => \'Select a catalog\' ); if ( $catalogs_terms ) { foreach ( $catalogs_terms as $catalog_term ) { $mycatalogs[$cata