1

I am trying to create a custom plugin in wordpress where i create a table and insert data using the pluging.Table is created successfully But data not inserted into the table. Here is my code

    global $jal_db_version;
    $jal_db_version = "1.0";

function jal_install() {
global $wpdb;
global $jal_db_version;

   $table_name = $wpdb->prefix . "demo1";

   $sql = "CREATE TABLE IF NOT EXISTS $table_name (
      id int(9) NOT NULL AUTO_INCREMENT,
      name varchar(255) NOT NULL,
      email varchar(255) NOT NULL,
      password varchar(255) NOT NULL,
      PRIMARY  KEY id (id)
    ) ENGINE=MyISAM  DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;";


   $wpdb->query($sql);
   pu_insert_custom_table($table_name);


}

function pu_insert_custom_table($table_name)
{
    global $wpdb;

    $wpdb->insert( 
        '$table_name', 
        array( 
            'name'=>'abc',
            'email'=>'abc@gmail.com',
            'password'=>'123456'
        ), 
        array( 
            '%s', 
            '%s',
            '%s'
        ) 
    );
}
  • Could you check the mysql log to see if there was a query to insert? – BenB Jan 08 '15 at 05:49
  • http://stackoverflow.com/questions/6479107/how-to-enable-mysql-query-log . Once you see if there was a query that failed or wasn't at all, you will have half of the answer. – BenB Jan 08 '15 at 05:51

2 Answers2

0

You need to modify your insert query as :

$wpdb->insert( 
        $table_name, 
        array( 
            'name'=>'abc',
            'email'=>'abc@gmail.com',
            'password'=>'123456'
        ), 
        array( 
            '%s', 
            '%s',
            '%s'
        ) 
    );
Jenis Patel
  • 1,617
  • 1
  • 13
  • 20
0

You can give it a try :

<?php

global $jal_db_version;
$jal_db_version = '1.0';

function jal_install() {
    global $wpdb;
    global $jal_db_version;

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

    $charset_collate = $wpdb->get_charset_collate();

    $sql = "CREATE TABLE $table_name (
        id mediumint(9) NOT NULL AUTO_INCREMENT,
        time datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
        name tinytext NOT NULL,
        text text NOT NULL,
        url varchar(55) DEFAULT '' NOT NULL,
        UNIQUE KEY id (id)
    ) $charset_collate;";

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

    add_option( 'jal_db_version', $jal_db_version );
}

function jal_install_data() {
    global $wpdb;

    $welcome_name = 'Mr. WordPres';
    $welcome_text = 'Congratulations, you just completed the installation!';

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

    $wpdb->insert( 
        $table_name, 
        array( 
            'time' => current_time( 'mysql' ), 
            'name' => $welcome_name, 
            'text' => $welcome_text, 
        ) 
    );
}

And then call these functions separately with wordpress hooks like this :

register_activation_hook( __FILE__, 'jal_install' );
register_activation_hook( __FILE__, 'jal_install_data' );

Taken from http://codex.wordpress.org/Creating_Tables_with_Plugins

Jenis Patel
  • 1,617
  • 1
  • 13
  • 20