• Resolved vijayj

    (@vijayj)


    I am trying to create a table for my plugin. But when I activate the plugin, the table is not creating.

    Attaching the code.

    function skoop_table () {
       global $wpdb;
    
       $table_name = $wpdb->prefix .'skoop_category'; 	
    	$charset_collate = $wpdb->get_charset_collate();
    
    	if($wpdb->get_var("SHOW TABLES LIKE '$table_name'" ) != $table_name){
        $sql= "CREATE TABLE $table_name (
               id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, 
               categoey VARCHAR(30) NOT NULL,
               time datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
               );";
        require_once (ABSPATH. 'wp-admin/includes/upgrade.php' );
        dbDelta($sql);
    }
    }
    
    register_activation_hook( __FILE__, 'skoop_table' );
    function add_skoop_data() {
    	global $wpdb;
    	
    	$categoey = '';
    	
    	$table_name = $wpdb->prefix .'skoop_category';
    	
    	$wpdb->insert( 
    		$table_name, 
    		array( 
    			'time' => current_time( 'mysql' ), 
    			'category' => '', 
    			
    		) 
    	);
    }
    
    register_activation_hook( __FILE__, 'add_skoop_data' );
    • This topic was modified 7 years, 7 months ago by vijayj.
Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter vijayj

    (@vijayj)

    There was a spelling mistake in the SQL query for table creation. Checked after correcting it. But the same result.

    • This reply was modified 7 years, 7 months ago by vijayj.
    Dion

    (@diondesigns)

    While it’s possible to call register_activation_hook multiple times, I’m not sure in which order those hooks will be executed. You may want to combine the code for those two hooks, and call register_activation_hook just once.

    Also, the query to create the table has errors. First, the final definition (“time”) cannot have a trailing comma. Second, “time” is a reserved word and must be escaped with backticks. (Give some thought to renaming the column.) And third, you should add the collation to the query because the table will otherwise be created with latin_swedish_ci collation.

    Thread Starter vijayj

    (@vijayj)

    Thanks, Dion Designs.It worked.

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Error in creating table in db for my plugin’ is closed to new replies.