how to create table when activating plugin in wordpress - php

i am begineer in wordpress , i want to create table when we activate a plugin .'
i have written a code for the same but it is not working . can any one find out what is wrong with this code . so that it will create table successfully
<?php
/*
Plugin Name: dbtable
*/
$tbl = $wpdb->prefix . 'member';
function myreg_func(){
global $wpdb;
global $tbl;
$sql="CREATE TABLE IF NOT EXISTS $tbl (id int not null AUTO_INCREMENT,
em varchar(255) NOT NULL,
mob BIGING NOT NULL ,
nm varchar (225) NOT NULL ,
PRIMARY KEY(id))";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
}
register_activation_hook(__FILE__,'myreg_func');
?>

<?php
/*
Plugin Name: check
*/
function plugin_name_activation() {
require_once( ABSPATH . '/wp-admin/includes/upgrade.php' );
global $wpdb;
$db_table_name = $wpdb->prefix . 'member';
$sql = "CREATE TABLE " . $db_table_name . " (
`id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT,
`em` varchar(100) NOT NULL ,
`mob` int(20) NOT NULL,
`nm` varchar (225) NOT NULL ,
PRIMARY KEY (`id`)
) ;";
dbDelta( $sql );
}
register_activation_hook(__FILE__, 'plugin_name_activation');
?>

Related

nothing happens when I run plugin

I wrote a plugin that included another plugin, but nothing happens when I run it. Please check this out and help me.
This is the first plugin, and this is the main plugin👇
<?php
/*
Plugin Name: DB Testsed
Description: a simple plugin
Version: 1.0
Author: Ak
Text Domain: HOooooooo
// */
include "ADT_db.php";
?>
This is the second plugin, which I added in the main plugin 👇
<?php
function scratchcode_create_payment_table()
{
global $wpdb;
$table_name = $wpdb->prefix . "ADT";
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE IF NOT EXISTS $table_name (
id bigint(20) NOT NULL AUTO_INCREMENT,
user_id bigint(20) UNSIGNED NOT NULL,
created_at datetime NOT NULL,
expires_at datetime NOT NULL,
PRIMARY KEY id (id)
) $charset_collate;";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
addMenu();
}
register_activation_hook(__FILE__,'scratchcode_create_payment_table');

Wordpress plugin activation creating table

I am trying to create a table with my wordpress plugin on activation. I have this and it is not working:
function activate_cron() {
global $wpdb;
$charset_collate = $wpdb->get_charset_collate();
$table_name = $wpdb->prefix . "fbcron";
$sql = "CREATE TABLE ".$table_name." (
id int(11) NOT NULL AUTO_INCREMENT,
post_id int(11) NOT NULL,
PRIMARY KEY (id)
) ".$charset_collate.";";
require_once( '../../../wp-admin/includes/upgrade.php' );
dbDelta($sql);
}
register_activation_hook( __FILE__, 'activate_cron' );
I believe for dbDelta the collate needs to be on its own line. It can be easier to make sure you have the right spacing by embedding the variables in the string:
$sql = "CREATE TABLE {$table_name} (
id int(11) NOT NULL AUTO_INCREMENT,
post_id int(11) NOT NULL,
PRIMARY KEY (id)
)
{$charset_collate};";
I think you have a problem with wp-admin/includes/upgrade.php path
I improve your existing code, tested
function activate_cron() {
global $wpdb;
$table_name = $wpdb->prefix . 'fbcron';
//check if table exist
if($wpdb->get_var("SHOW TABLES LIKE '$table_name';") != $table_name) {
$sql = "CREATE TABLE ".$table_name." (
id int(11) NOT NULL AUTO_INCREMENT,
post_id int(11) NOT NULL,
PRIMARY KEY (id)
) ".$charset_collate.";";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); //this path
dbDelta($sql);
}
}
register_activation_hook( __FILE__, 'activate_cron' );
Hope it helps

insert table in WordPress database

I want to insert a table into WordPress database while plugin activated. I have used this code but it is not working. This code should create a table while plugin gets activated, but in my WordPress when I activate the plugin, the code works, but table is not being created in the database.
How can I create the table in WordPress database while plugin activated?
I have use the below code for my plugin
class createtabel {
function __construct() {
register_activation_hook( __FILE__, 'create_plugin_database_table' );
}
public static function create_plugin_database_table() {
global $wpdb;
$table_name = $wpdb->prefix . 'wp_productqa';
$sql = "CREATE TABLE $table_name (
QA_id mediumint(9) unsigned NOT NULL AUTO_INCREMENT,
Product_ID varchar(50) NOT NULL,
Username varchar(50)NOT NULL,
Useremail varchar(50) NOT NULL,
comment varchar(255) NOT NULL,
PRIMARY KEY (QA_id)
);";
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
dbDelta( $sql );
}
}
You have to make sure register_activation_hook is called.
For instance, in your plugin definition file you may:
$createTable = new createtabel;
register_activation_hook(__FILE__, array($createTable, 'create_plugin_database_table'));
BTW, in the createtabel class you can create your table with:
$wpdb->query($sql);
Is that your intention also to have
$table_name = $wpdb->prefix . 'wp_productqa';
which in most likelihood (assuming prefix is wp_)will have:
$table_name = 'wp_wp_productqa';
Please use this code while activate your plugin it will create new table in your database.
register_activation_hook(__FILE__, 'install_covercarousel');
Define this above hook in globally where your file coding has been started.
Also please dont' use public static function just add function and functioname like this function abs(){}
After changed code look like this
register_activation_hook( __FILE__, 'create_plugin_database_table' );
function create_plugin_database_table() {
global $wpdb;
$table_name = $wpdb->prefix . 'wp_productqa';
$sql = "CREATE TABLE $table_name (
QA_id mediumint(9) unsigned NOT NULL AUTO_INCREMENT,
Product_ID varchar(50) NOT NULL,
Username varchar(50)NOT NULL,
Useremail varchar(50) NOT NULL,
comment varchar(255) NOT NULL,
PRIMARY KEY (QA_id)
);";
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
dbDelta( $sql );
}

how to create a table inside plugin in wordpress database

I want to create a table in existing wordpress database.and i am unable to create it . I have write this code inside a custom plugin files.Please help me through this code.
<?php
/**
* #package Demo
*/
/*
Plugin Name: Demo
Plugin URI: https://demo.com/
Description: This is used for the plugin database.
Version: 3.2
Author: Automattic
Author URI: https://automattic.com/wordpress-plugins/
License: GPLv2 or later
Text Domain: demo
*/
// run the install scripts upon plugin activation
register_activation_hook(__FILE__,'your_plugin_options_install');
// function to create the DB / Options / Defaults
function your_plugin_options_install() {
global $wpdb;
global $db_version;
$your_db_name = $wpdb->prefix . 'db_record';
// create the ECPT metabox database table
$sql = "CREATE TABLE " . $your_db_name . " (
`id` mediumint(9) NOT NULL AUTO_INCREMENT,
`field_1` mediumtext NOT NULL,
`field_2` tinytext NOT NULL,
`field_3` tinytext NOT NULL,
`field_4` tinytext NOT NULL,
UNIQUE KEY id (id)
);";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
add_option("db_version", $db_version);
}
?>
function wpa_install() {
/*Create Table on Plugin Activation*/
global $wpdb;
$table_name = $wpdb->prefix . "table_name";
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE IF NOT EXISTS $table_name (
`id` int(11) NOT NULL AUTO_INCREMENT,
`records` text COLLATE utf8mb4_unicode_ci NOT NULL,
`loggedin_time` datetime NOT NULL,
`unique_number` varchar(8) COLLATE utf8mb4_unicode_ci NOT NULL,
PRIMARY KEY (`id`)
) $charset_collate";
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
dbDelta( $sql );
/*Create Table on Plugin Activation*/
}
class WP_DiscoverU {
// Constructor
function __construct()
register_deactivation_hook(__FILE__, array($this, 'wpa_uninstall'));
//call function when activate plugin
}
}
Above code for generate table when activate plugin.

Unable to create a table in existing wordpress database

I want to create a table in existing wordpress database and I am unable to create it. I have write this code inside a custom plugin files. Please help me through this code.
function create_xml_table(){
global $wpdb;
$tableName = $wpdb->prefix."XMLdata";
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE $tableName(id int(10) NOT NULL AUTO_INCREMENT,
user_id varchar(255) NOT NULL,
post_id varchar(255) NOT NULL,
value varchar(255) DEFAULT '' NOT NULL,
time timestamp ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (id)
) $charset_collate;";
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
dbDelta( $sql );
register_activation_hook(__FILE__, 'create_xml_table');
}
You have to do like this.
function create_xml_table(){
global $wpdb;
$tableName = $wpdb->prefix."XMLdata";
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE $tableName(id int(10) NOT NULL AUTO_INCREMENT,
user_id varchar(255) NOT NULL,
post_id varchar(255) NOT NULL,
value varchar(255) DEFAULT '' NOT NULL,
time timestamp ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (id)
)$charset_collate;";
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
dbDelta( $sql );
}
register_activation_hook(__FILE__, 'create_xml_table');

Categories