Cannot create table in wordpress - php

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

Related

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

Wordpress plugin is not creating table when activating

I am working to develop a plugin that needs to create a table in database here is my code please help
the code is not showing any errors and not creating the table
public function create_phone_order_db() {
global $wpdb;
$charset_collate = $wpdb->get_charset_collate();
$table_name = $wpdb->prefix . 'phone_orders';
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
$sql = "CREATE TABLE IF NOT EXISTS " . $table_name . " (
id INT NOT NULL AUTO_INCREMENT,
billing_country VARCHAR(100) NOT NULL,
billing_first_name VARCHAR(50) NOT NULL,
billing_last_name VARCHAR(50) NOT NULL,
billing_company VARCHAR(50) NOT NULL,
billing_address_1 VARCHAR(120) NOT NULL,
billing_address_2 VARCHAR(120) NOT NULL,
billing_city VARCHAR(50) NOT NULL,
billing_state VARCHAR(40) NOT NULL,
billing_postcode VARCHAR(8) NOT NULL,
billing_email VARCHAR(25) NOT NULL,
billing_phone VARCHAR(20) NOT NULL,
ship_to_different_address VARCHAR(10) NOT NULL,
shipping_country VARCHAR(50) NOT NULL,
shipping_first_name VARCHAR(40) NOT NULL,
shipping_last_name VARCHAR(40) NOT NULL,
shipping_company VARCHAR(50) NOT NULL,
shipping_address_1 VARCHAR(120) NOT NULL,
shipping_address_2 VARCHAR(120) NOT NULL,
shipping_city VARCHAR(50) NOT NULL,
shipping_state VARCHAR(50) NOT NULL,
shipping_postcode VARCHAR(8) NOT NULL,
admin_email_switched VARCHAR(25) NOT NULL,
admin_switched_id INT NOT NULL,
admin_user_name_switched VARCHAR(30) NOT NULL,
admin_role_switched VARCHAR(15) NOT NULL,
order_comments VARCHAR NOT NULL,
shipping_method VARCHAR NOT NULL,
payment_method VARCHAR NOT NULL,
_wpnonce VARCHAR NOT NULL,
_wp_http_referer VARCHAR NOT NULL,
PRIMARY KEY (id)
) " . $charset_collate . ";";
dbDelta($sql);
}
register_activation_hook( FILE, array($this, 'create_phone_order_db'));
According to WordPress Codex you should do
class MyPlugin {
static function install() {
global $wpdb;
$table_name = $wpdb->prefix . 'my_table';
$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 );
}
}
register_activation_hook(__FILE__, array( 'MyPlugin', 'install' ) );
You can read more here on creating a table on plugin activation.

Wordpress table not being created

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.

Using PHP to create two tables in the WordPress database when plugin is activated

I am having a problem with my plugin that is suppose to create a table in the WordPress database when it is activated. My current code is as follows:
register_activation_hook(__FILE__, 'wp_table_install');
function wp_table_install(){
global $wpdb;
global $db_version;
$sql ="CREATE TABLE IF NOT EXISTS 'st_support_tickets'
('id' mediumint(8) unsigned NOT NULL auto_increment,
'ticket_id' varchar(36) NOT NULL,
'ticket_user_id' varchar(36) NOT NULL,
'ticket_description' TEXT default NULL,
'ticket_priority' varchar(255) default NULL,
'ticket_status' varchar(255) default NULL,
'ticket_type' varchar(255) default 'Private',
PRIMARY KEY ('id'))
AUTO_INCREMENT=1;
CREATE TABLE IF NOT EXISTS 'st_support_priorities'
('id' mediumint(8) unsigned NOT NULL auto_increment,
'ticket_user_id' varchar(36) NOT NULL,
PRIMARY KEY ('id'))
AUTO_INCREMENT=1;
";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
add_option("db_version", $db_version);
}
What is stopping these tables from being created? Is it the sql query or the syntax or what?
To solve this problem I remove all ' characters from the sql statements excluding around default values. Eg:
$sql1 ="CREATE TABLE IF NOT EXISTS st_support_tickets
(id mediumint(8) unsigned NOT NULL auto_increment,
ticket_user_id varchar(36) NOT NULL,
ticket_description TEXT,
ticket_priority varchar(255) default NULL,
ticket_status varchar(255) default NULL,
ticket_type varchar(255) default 'Private',
PRIMARY KEY ( id ))
AUTO_INCREMENT=1;
";
$sql2 = "CREATE TABLE IF NOT EXISTS st_support_priorities
( id mediumint(8) unsigned NOT NULL auto_increment,
ticket_priority varchar(36) NOT NULL,
PRIMARY KEY ( id ))
AUTO_INCREMENT=1;
";
Had no problems after this.
register_activation_hook(__FILE__, 'wp_table_install');
function wp_table_install(){
global $wpdb;
global $db_version;
$sql ="CREATE TABLE IF NOT EXISTS 'st_support_tickets'
('id' mediumint(8) unsigned NOT NULL auto_increment,
'ticket_id' varchar(36) NOT NULL,
'ticket_user_id' varchar(36) NOT NULL,
'ticket_description' TEXT,
'ticket_priority' varchar(255) default NULL,
'ticket_status' varchar(255) default NULL,
'ticket_type' varchar(255) default 'Private',
PRIMARY KEY ('id'))
AUTO_INCREMENT=1;
CREATE TABLE IF NOT EXISTS 'st_support_priorities'
('id' mediumint(8) unsigned NOT NULL auto_increment,
'ticket_user_id' varchar(36) NOT NULL,
PRIMARY KEY ('id'))
AUTO_INCREMENT=1;
";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
add_option("db_version", $db_version);
}
Try with this
$sql1 ="CREATE TABLE IF NOT EXISTS 'st_support_tickets'
('id' mediumint(8) unsigned NOT NULL auto_increment,
'ticket_id' varchar(36) NOT NULL,
'ticket_user_id' varchar(36) NOT NULL,
'ticket_description' TEXT,
'ticket_priority' varchar(255) default NULL,
'ticket_status' varchar(255) default NULL,
'ticket_type' varchar(255) default 'Private',
PRIMARY KEY ('id'))
AUTO_INCREMENT=1";
$sql2 ="CREATE TABLE IF NOT EXISTS 'st_support_priorities'
('id' mediumint(8) unsigned NOT NULL auto_increment,
'ticket_user_id' varchar(36) NOT NULL,
PRIMARY KEY ('id'))
AUTO_INCREMENT=1;
";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
dbDelta($sq2);
Tested and works fine..
register_activation_hook(__FILE__, 'wp_table_install');
function wp_table_install(){
global $wpdb;
global $db_version;
$charset_collate = $wpdb->get_charset_collate();
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
$sql ="CREATE TABLE IF NOT EXISTS st_support_tickets (
id mediumint(8) unsigned NOT NULL AUTO_INCREMENT,
ticket_id varchar(36) DEFAULT '' NOT NULL,
ticket_user_id varchar(36) DEFAULT '' NOT NULL,
ticket_description TEXT DEFAULT '' NOT NULL,
ticket_priority varchar(255) DEFAULT '' NOT NULL,
ticket_status varchar(255) DEFAULT '' NOT NULL,
ticket_type varchar(255) DEFAULT 'Private' NOT NULL,
PRIMARY KEY (id)) $charset_collate;";
dbDelta( $sql );
$sql="CREATE TABLE IF NOT EXISTS st_support_priorities (
id mediumint(8) unsigned NOT NULL AUTO_INCREMENT,
ticket_user_id varchar(36) DEFAULT '' NOT NULL,
PRIMARY KEY (id)) $charset_collate;";
dbDelta($sql);
add_option("db_version", $db_version);
}

Categories