Wordpress table not being created - php

I have been trying to create a new table for my wordpress plugin for a few hours now without success.
My best guess is that there is some error in the sql which i cannot find. I have been trying to look at the wordpress codex and other sources as well without success. Would apprechiate some help!
register_activation_hook( __FILE__, 'plugin_install' );
function plugin_install(){
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
global $wpdb;
$table_name = $wpdb->prefix . 'dbname';
$charset_collate = $wpdb->get_charset_collate();
$sql_create_table = "CREATE TABLE $table_name (
id mediumint(9) NOT NULL PRIMARY KEY AUTO_INCREMENT,
b_id int(11) NOT NULL,
iprefix varchar(20) NOT NULL default '0',
istart bigint(20) NOT NULL default 'updated',
iend bigint(20) unsigned NOT NULL default '0',
isuffix varchar(20) NOT NULL default 'post',
PRIMARY KEY (id)
) $charset_collate; ";
dbDelta( $sql_create_table );
}

In the fourth line of your create SQL code you have an unsigned varchar. Remove the unsigned keyword and it should be fine.

Related

Cannot create table in wordpress

I cannot create table in wordpress, i'm not understand why?
private function create($prefix)
{
$sql = "CREATE TABLE " . $prefix . "submit (
submit_id bigint PRIMARY KEY AUTO_INCREMENT,
post_id bigint,
user_id bigint,
author text,
user_email text,
source text,
pass text,
language text,
time datetime,
CONSTRAINT post_id FOREIGN KEY (post_id) REFERENCES wp_posts(ID),
CONSTRAINT user_id FOREIGN KEY (user_id) REFERENCES wp_users(ID)
)";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
var_dump(dbDelta($sql));
}
The var_dump() shows:
array(1) { ["wp_submit"]=> string(23) "Created table wp_submit" }
add_action("after_switch_theme", "my_custom_table_setup");
function my_custom_table_setup()
{
global $wpdb;
$create_table_query = "
CREATE TABLE {$wpdb->prefix}products_by_sku (
id int(11) NOT NULL auto_increment,
product_id int(11) NOT NULL,
lot_id varchar(25) NOT NULL,
title varchar(255) NOT NULL,
LP varchar(25) NOT NULL,
catgeory varchar(255) NOT NULL,
product_name varchar(255) NOT NULL,
UPC varchar(255) DEFAULT NULL,
ASIN varchar(255) DEFAULT NULL,
original_retail_price float DEFAULT NULL,
quantity int(11) DEFAULT NULL,
total_original_retail float NOT NULL DEFAULT 0,
stock_image text,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
";
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
dbDelta( $create_table_query );
}
Try this code.
Thank you.
You can use this code:
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,
PRIMARY KEY (id)
) $charset_collate;";
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
dbDelta( $sql );
add_option( 'jal_db_version', $jal_db_version );
}
register_activation_hook( __FILE__, 'jal_install' );

global variable not working inside function

In a wordpress plugin I have this code
$country_table = $wpdb->prefix . "trackbyid_country";
$cities_table = $wpdb->prefix . "trackbyid_cities";
function database_creation(){
global $wpdb;
global $country_table;
global $cities_table;
$AO_name = "trackbyid_dbv";
$AO_value = "1";
add_option($AO_name, $AO_value);
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE $country_table (
id mediumint(9) NOT NULL AUTO_INCREMENT,
time datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
country tinytext NOT NULL,
PRIMARY KEY (id)
) $charset_collate;";
require_once ( ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
$sql = "CREATE TABLE $cities_table(
id mediumint(9) NOT NULL AUTO_INCREMENT,
time datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
city tinytext NOT NULL,
country_id mediumint(9) NOT NULL,
city_latitude mediumint(9) NOT NULL,
city_longitude mediumint(9) NOT NULL,
PRIMARY KEY (id)
) $charset_collate;";
dbDelta($sql);
}
I think my problem (Database is not being created) is with global $country_table and $cities_table variables. when I make that variables as local the problem solves!
whats wrong with my global variable declaration and usage?
Your variables $country_table and $cities_table won't be defined because you didn't bring in the global $wpdb before you tried to use it in those vars. Try this instead:
global $wpdb;
$country_table = $wpdb->prefix . "trackbyid_country";
$cities_table = $wpdb->prefix . "trackbyid_cities";
function database_creation(){
global $country_table, $cities_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

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');

Wordpress Plugin Create Database Table

Trying to create a pair of database tables within my table. Below is the code block that is being executed on the activation of my plugin. Wordpress reports that it's successful, however, when refreshing the database the employee table is not being created. The department table however is created successfully.
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
// create the database table.
global $wpdb;
$charset_collate = $wpdb->get_charset_collate();
$table_name = $wpdb->prefix . "plugin_department";
$table_name2 = $wpdb->prefix . "plugin_employee";
$sql = "CREATE TABLE $table_name (
id INT(11) NOT NULL AUTO_INCREMENT,
name VARCHAR(255) NOT NULL,
PRIMARY KEY (id)
) $charset_collate;";
$sql2 = "CREATE TABLE $table_name2 (
id INT(11) NOT NULL AUTO_INCREMENT,
first_name VARCHAR(255) NOT NULL,
last_name VARCHAR(255) NOT NULL,
department_id INT(11) NOT NULL,
PRIMARY KEY (id),
CONSTRAINT department_id
FOREIGN KEY (id)
REFERENCES plugin_department (id)
ON DELETE NO ACTION
ON UPDATE NO ACTION);
) $charset_collate;";
dbDelta($sql);
dbDelta($sql2);
Ideas on how to resolve would be greatly appreciated.
Discovered the issue, using mysql workbench I had it generate the create table script for me, then was a simple copy and paste into the $sql2 variable. My assumption is that there must be a wonky character or a syntax error that is hard for my eyes to pick out.
$sql2 = "CREATE TABLE $table_name2 (
`id` INT NOT NULL AUTO_INCREMENT,
`first_name` VARCHAR(255) NOT NULL,
`last_name` VARCHAR(45) NOT NULL,
`department_id` INT NOT NULL,
PRIMARY KEY (`id`),
INDEX `wp_plugin_employee_department_idx` (`department_id` ASC),
CONSTRAINT `wp_plugin_employee_department`
FOREIGN KEY (`department_id`)
REFERENCES `plugin_intranet`.`wp_plugin_department` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)";
Create Table
global $wpdb;
if ($wpdb->get_var("SHOW TABLES LIKE '{$wpdb->prefix}tabloadi'") != $wpdb->prefix . 'tabloadi'){
$wpdb->query("CREATE TABLE {$wpdb->prefix}tabloadi(
id integer not null auto_increment,
alan1 TINYTEXT CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
alan2 TINYTEXT CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
alan3 TINYTEXT CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
alan4tarih TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (id)
);");
}
SELECT
global $wpdb;
$tabloadi = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}tabloadi WHERE sart1=$deger1" );
foreach($tabloadi as $row)
{
echo $row->id;
echo $row->alan1;
echo $row->tarih;
}
Delete
global $wpdb;
$delete = $wpdb->delete($wpdb->prefix.'tabloadi',array('alan1'=>$alan1deger,'alan2'=>$alan2deger));

Categories