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.
Related
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' );
I am working on creating table in to my database with mysql and here is my code:
mysqli_query($link, "CREATE TABLE IF NOT EXISTS `review` (
`clients_id` int(15) NOT NULL AUTO_INCREMENT,
`client_id` varchar(150) NOT NULL DEFAULT '',
`rating` tinyint(2) NOT NULL DEFAULT '0',
`proj_date` date NOT NULL DEFAULT '0000-00-00',
`proj_desc` text NOT NULL DEFAULT '',
`photoname` text NOT NULL,
`companyname` text NOT NULL,
`feedback` text NOT NULL,
`status` tinyint(1) NOT NULL DEFAULT '0',
`emailid` varchar(100) NOT NULL DEFAULT '',
`customratings` varchar(100) NOT NULL DEFAULT '',
`photo_option` varchar(100) NOT NULL DEFAULT '',
`title` varchar(100) NOT NULL DEFAULT '',
`citation` varchar(100) NOT NULL DEFAULT '',
`date_option` varchar(100) NOT NULL DEFAULT '',
`rating_option` varchar(100) NOT NULL DEFAULT '',
PRIMARY KEY (`clients_id`),
FULLTEXT KEY `feedback` (`feedback`)
) ENGINE=MyISAM AUTO_INCREMENT=1") or mysqli_error($link);
But this is not reflecting into my database ? Why were I may be going wrong ?
but I tried creating other table with the following code
mysqli_query($link, "CREATE TABLE IF NOT EXISTS `setting` (
`id` int(11) NOT NULL auto_increment,
`script_url` text NOT NULL,
`date` varchar(4) NOT NULL,
`rateing` varchar(4) NOT NULL,
`photo` varchar(4) NOT NULL,
`dateformat` varchar(4) NOT NULL,
`page_limit` int(4) NOT NULL,
`proj_desc` varchar(4) NOT NULL,
`companyname` varchar(4) NOT NULL,
`text_color` varchar(255) NOT NULL,
`citation_color` varchar(255) NOT NULL,
`bg_color` varchar(255) NOT NULL,
`border_color` varchar(255) NOT NULL,
`ratingsformat` varchar(250) NOT NULL,
`rating` varchar(250) NOT NULL,
`customratings` varchar(250) NOT NULL,
`speed` varchar(250) NOT NULL,
`pagination` varchar(250) NOT NULL,
`version` varchar(250) NOT NULL,
`global_option` varchar(250) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=0;
") or mysqli_error($link);
it is being created correctly and both the tables are in the same file
No you cannot create table using an insert statement.
There are four types of SQL statements:
DML (DATA MANIPULATION LANGUAGE)
DDL (DATA DEFINITION LANGUAGE)
DCL (DATA CONTROL LANGUAGE)
TCL (TRANSACTION CONTROL LANGUAGE)
DML is your SELECT, INSERT, UPDATE and DELETE statements...
DDL is you your CREATE, ALTER and DROP statements.
See more info about types of SQL statements
In order to insert data in your table, first you need to create it.
How to create sql table from php
No. "INSERT INTO" used to insert the data to existing table only.You should create the table with respective column before try to insert the values.
Or you can check the table exist or not before going insert."If exists" u can insert the value else just create and insert the values.
This worked for me but I don't know how?
I just removed DEFAULT '' present while creating review table and table just got created
Here is edited code
mysqli_query($link, "CREATE TABLE IF NOT EXISTS `review` (
`clients_id` int(15) NOT NULL AUTO_INCREMENT,
`client_id` varchar(150) NOT NULL,
`rating` tinyint(2) NOT NULL,
`proj_date` date NOT NULL,
`proj_desc` text NOT NULL,
`photoname` text NOT NULL,
`companyname` text NOT NULL,
`feedback` text NOT NULL,
`status` tinyint(1) NOT NULL,
`emailid` varchar(100) NOT NULL,
`customratings` varchar(100) NOT NULL,
`photo_option` varchar(100) NOT NULL,
`title` varchar(100) NOT NULL,
`citation` varchar(100) NOT NULL,
`date_option` varchar(100) NOT NULL,
`rating_option` varchar(100) NOT NULL,
PRIMARY KEY (`clients_id`),
FULLTEXT KEY `feedback` (`feedback`)
) ENGINE=MyISAM AUTO_INCREMENT=1") or mysqli_error($link);
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);
}
I have a lead table that contains data which also contains information which user has entered this data. Now I want to create a filter that when selecting a particular user only his data is displayed. How to do this in cakephp. Please help.
Thanks in advance
User database
CREATE TABLE IF NOT EXISTS `users` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`fname` varchar(100) DEFAULT NULL,
`lname` varchar(100) DEFAULT NULL,
`username` varchar(100) NOT NULL,
`password` varchar(50) NOT NULL,
`email` varchar(100) DEFAULT NULL,
`phone` varchar(11) DEFAULT NULL,
`role` varchar(100) NOT NULL,
`created` datetime DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `username` (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;
lead table
CREATE TABLE IF NOT EXISTS `leads` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`account_id` int(11) NOT NULL,
`name` varchar(200) DEFAULT NULL,
`email` varchar(100) DEFAULT NULL,
`board_number` varchar(11) DEFAULT NULL COMMENT 'board number',
`mobile_number` varchar(11) DEFAULT NULL COMMENT 'mobile number',
`requirements` varchar(1000) DEFAULT NULL,
`total_price_quoted` int(11) DEFAULT NULL COMMENT 'total price quoted',
`our_price` int(11) DEFAULT NULL COMMENT 'our price',
`margin` int(11) DEFAULT NULL,
`closing_month` varchar(100) DEFAULT NULL COMMENT 'closing month',
`probablity` varchar(100) DEFAULT NULL,
`status_id` int(11) NOT NULL,
`user_id` int(11) NOT NULL,
`date_added` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=24 ;
Controller Code LeadController.php
public function user_filter() {
//$this->render('filter');
$this->loadModel('User');
$this->User->recursive = 0;
$userFilter = $this->User->find('all',array(
'fields' => array('User.username')
));
$this->set('userFilter', $userFilter);
return $userFilter;
}
// View/filterMenu.ctp
//here a dropdown menu of listed user
<?php
$userFilters = $this->requestAction(
array(
'controller' => 'leads',
'action' => 'user_filter'
)
);
?>
<ul class="dropdown-menu">
<?php foreach ($userFilters as $userFilter): ?>
<li>
<?php
echo $this->Html->link($userFilter['User']['username'],array(
'controller'=>'leads',
'action'=>'user_filter'
));
echo "<\n>";
?>
</li>
<?php endforeach; ?>
</ul
I want to create view of that particular user.
you cannot provide links through dropdown, you must use javascript to direct to page.
to display all the leads from particular user, pass the user id to the function and fetch data using user id.
public function user_filter($id = null) {
if ($id){
$leads = $this->Lead->find('all', array('conditions' => array('Lead.user_id' => $id)));
}else{
$leads = $this->Lead->find('all');
}
$this->loadModel('User');
$this->User->recursive = 0;
$userFilter = $this->User->find('all',array(
'fields' => array('User.username')
));
$this->set('leads', $leads);
$this->set('userFilter', $userFilter);
return $userFilter;
}
Hope this will help you.
I want to create a table into the database upon installing the plugin I've created.
In my main plugin file (index.php):
register_activation_hook(__FILE__, 'wnm_install');
global $wnm_db_version;
$wnm_db_version = "1.0";
function wnm_install(){
global $wpdb;
global $wnm_db_version;
$sql = "CREATE TABLE tbl_campaigns (
campaignID int(11) NOT NULL AUTO_INCREMENT,
campaign_name varchar(128) NOT NULL,
start_duration date NOT NULL,
end_duration date NOT NULL,
activity varchar(500) NOT NULL,
survey_settings varchar(50) NOT NULL,
limit varchar(50) NOT NULL,
goal varchar(100) DEFAULT NULL,
PRIMARY KEY (campaignID)
) ;";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
add_option("wnm_db_version", $wnm_db_version);
}
I just followed the instructions from this http://codex.wordpress.org/Creating_Tables_with_Plugins
But it doesn't work.
What seems to be the problem with this code?
limit varchar(50) NOT NULL,
Limit is a keyword, change to something else like
`limit` varchar(50) NOT NULL,
Use back ticks around keywords