0

I'm trying to update my database by adding an extra column field. However, dBdelta does not seem to be doing its job. I'm not sure what am I doing wrong.

<?php

function installer(){
    include('installer.php');
}
register_activation_hook( __file__, 'installer' ); //executes installer php when installing plugin to create new database

//database update checkdate
function myplugin_update_db_check() {
    global $xenonresult_db_version;
    if ( get_site_option( 'xenonresult_db_version' ) != $xenonresult_db_version ) {
        installer();
    }
}
add_action( 'plugins_loaded', 'myplugin_update_db_check' );

Here is the installer.php

<?php
    global $wpdb;
    $table_name = $wpdb->prefix . "xenonresult";
    $xenonresult_db_version = '1.1';
    $charset_collate = $wpdb->get_charset_collate();

    if ( $wpdb->get_var( "SHOW TABLES LIKE '{$table_name}'" ) != $table_name ) {
        $sql = "CREATE TABLE IF NOT EXISTS $table_name (
        `id` int(11) NOT NULL AUTO_INCREMENT,
        `roll_number` int(11) NOT NULL,
        `student_name` text NOT NULL,
        `father_name` text NOT NULL,
        `obj_marks` int(9) NOT NULL,
        `sub_marks` int(9) NOT NULL,
        `result` text NOT NULL,
        PRIMARY KEY  (`id`)
        )    $charset_collate;";
        require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
        dbDelta( $sql );
        add_option( "xenonresult_db_version", $xenonresult_db_version );
    }

global $wpdb;
$installed_ver = get_option( "xenonresult_db_version" );
if ( $installed_ver != $xenonresult_db_version ) {

    $table_name = $wpdb->prefix . 'xenonresult';

    $sql = "CREATE TABLE IF NOT EXISTS $table_name (
        `id` int(11) NOT NULL AUTO_INCREMENT,
        `roll_number` int(11) NOT NULL,
        `student_name` text NOT NULL,
        `father_name` text NOT NULL,
        `obj_marks` int(9) NOT NULL,
        `sub_marks` int(9) NOT NULL,
        `result` text NOT NULL,
        `mobile` varchar NOT NULL,
        PRIMARY KEY  (`id`)
        )    $charset_collate;";
        require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
        dbDelta( $sql );

    update_option( "xenonresult_db_version", $xenonresult_db_version );
}
?>

My default version is 1.0. In 1.1, I'm trying to add mobile column in my database.

1

After trial and error, here is the code that works:

function installer(){
    include('installer.php');
}
register_activation_hook( __file__, 'installer' ); //executes installer php when installing plugin to create new database

//database update checkdate
function myplugin_update_db_check() {
    global $xenon_result_db_version;
    if ( get_option( 'xenon_result_db_version' ) != $xenon_result_db_version ) {
        installer();
    }
}

add_action( 'plugins_loaded', 'myplugin_update_db_check' );

The installer.php looks somewhat like this:

<?php
    global $wpdb;
    $table_name = $wpdb->prefix . "xenon_result";
    $xenon_result_db_version = '1.2';
    $charset_collate = $wpdb->get_charset_collate();
    if ( $wpdb->get_var( "SHOW TABLES LIKE '{$table_name}'" ) != $table_name ) {
        $sql = "CREATE TABLE $table_name (
        `id` int(11) NOT NULL AUTO_INCREMENT,
        `roll_number` int(11) NOT NULL,
        `student_name` text NOT NULL,
        `father_name` text NOT NULL,
        `mobile` varchar(55) NOT NULL,
        `obj_marks` int(9) NOT NULL,
        `sub_marks` int(9) NOT NULL,
        `total_marks` int(9) NOT NULL,
        `result` text NOT NULL,
        PRIMARY KEY  (`id`)
        )    $charset_collate;";
        require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
        dbDelta( $sql );
        add_option( "xenon_result_db_version", $xenon_result_db_version );
}
if ( get_option( 'xenon_result_db_version' ) != $xenon_result_db_version ){
$sql = "CREATE TABLE $table_name (
        `id` int(11) NOT NULL AUTO_INCREMENT,
        `roll_number` int(11) NOT NULL,
        `student_name` text NOT NULL,
        `father_name` text NOT NULL,
        `mobile` varchar(55) NOT NULL,
        `obj_marks` int(9) NOT NULL,
        `sub_marks` int(9) NOT NULL,
        `total_marks` int(9) NOT NULL,
        `result` text NOT NULL,
        PRIMARY KEY  (`id`)
        )    $charset_collate;";
        require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
        dbDelta( $sql );
        update_option( "xenon_result_db_version", $xenon_result_db_version );
}
?>

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.