So I want to create a new table in my database and I can't seem to get it working. It's a wordpress plugin so I don't know if that's what could be messing it up.
if ($wpdb->get_var('SHOW TABLES LIKE "' . $wpdb->prefix . DB_PROFILE_TABLE . '"') != $wpdb->prefix . DB_PROFILE_TABLE)
{
$sql = 'CREATE TABLE ' . $wpdb->prefix . DB_PROFILE_TABLE . ' (
id BIGINT(10) UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT,
fname VARCHAR(100) NOT NULL,
sname VARCHAR(100),
city VARCHAR(100),
country VARCHAR(100)
CHARSET=utf8; ';
dbDelta($sql);
}
Beginner at php and mySql.
You are missing parenthesis...
Can you try in this way
global $wpdb;
$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 );
These are the rules to create table using wordpress plugin
1) You must put each field on its own line in your SQL statement.
2) You must have two spaces between the words PRIMARY KEY and the definition of your primary key.
3) You must use the key word KEY rather than its synonym INDEX and you must include at least one KEY.
4) You must not use any apostrophes or backticks around field names.
Field types must be all lowercase.
5) SQL keywords, like CREATE TABLE and UPDATE, must be uppercase.
Refer http://codex.wordpress.org/Creating_Tables_with_Plugins
Looks like you are missing a closing parenthesis after country VARCHAR(100). You may also need to remove the semi-colon as well.
$sql = 'CREATE TABLE ' . $wpdb->prefix . DB_PROFILE_TABLE . ' (
id BIGINT(10) UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT,
fname VARCHAR(100) NOT NULL,
sname VARCHAR(100),
city VARCHAR(100),
country VARCHAR(100))
CHARSET=utf8';
dbDelta($sql);
Related
$tablename = $wpdb->prefix . "name";
$sql = "CREATE TABLE IF NOT EXISTS $tablename(
time_id INT(11) NOT NULL AUTO_INCREMENT,
job VARCHAR(120) NOT NULL,
employee VARCHAR(100) NOT NULL,
date_entry DATETIME NOT NULL,
duration INT(11) NOT NULL,
PRIMARY KEY(time_id)
);";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
creates two tables: 'wp_name' and 'name'. If I define $tablename = 'wp_name' instead it only creates one table. Also '{$wpdb->prefix}name' doesn't change the problem.
What's the problem here? - thanks!
Get rd of the brackets
$sql = "CREATE TABLE IF NOT EXISTS $tablename (
...
);";
They are only used to replace internal wp variables
Update:
your case can't create two tables by itself, there is no way, you have somewhere the same code twice.
Without the prefix, the second code checks that there is already a table named nameand does nothing, but when you add the prefix, it tries to add the tbale name which azt this time doesn't exist, because it is call wp_name
I am trying to create a table whenever the user submits his/her domain name. It is definitely going to be a .com or a .net or a .something
The problem is that my code does not create a table when it contains a .anything
it works fine for names and characters without a .something
$domain=$_POST['domain_name'];//it is dramatainment.com
$table = mysqli_query($connection, "CREATE TABLE $domain (
user_id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
user_name VARCHAR(100) NOT NULL,
user_domain VARCHAR(50) NOT NULL,
user_email VARCHAR(50),
user_password VARCHAR(50),
user_date date
) ");
if(!$table)
{
die('Could not create table: ' . mysqli_error($connection));
}
Does the creat table command have this limitation? Can this be solved?
You could try to replace the dot by an underscore.
Here you can find which character is allowed in a table name.
Additionally, it is possible to query using this syntax, which would conflict with your table name :
SELECT * FROM dbname.table_name;
UPDATE : it is possible using backticks to enclose table name :
$sql = 'CREATE TABLE `$tableName` (
user_id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
user_name VARCHAR(100) NOT NULL,
user_domain VARCHAR(50) NOT NULL,
user_email VARCHAR(50),
user_password VARCHAR(50),
user_date date
)';
Some special characters are invalid for identifiers such as table names.
See http://dev.mysql.com/doc/refman/5.7/en/identifiers.html
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));
So it says that I have to put a double space between PRIMARY KEY. Does this mean that I have to put a double space between PRIMARY and KEY. Also, what does it mean to put it between the definition of your PRIMARY KEY too? Thanks!
You must put each field on its own line in your SQL statement.
You must have two spaces between the words PRIMARY KEY and the definition of your primary key.
You must use the key word KEY rather than its synonym INDEX and you must include at least one KEY.
You must not use any apostrophes or backticks around field names.
Field types must be all lowercase
SQL keywords, like CREATE TABLE and UPDATE, must be uppercase.
global $wpdb;
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE $table_name (
id mediumint(9) NOT NULL PRIMARY KEY 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 );
This is a good question, and no, no need for an extra space between PRIMARY KEY. The dbDelta() function can be precarious about the SQL statement served as a parameter so don't use PRIMARY KEY declaration as you would normally. The primary key is declared (PRIMARY KEY) and then the definition (id). You should add the primary key as follows:
global $wpdb;
$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 );
global $wpdb;
$table_name = $wpdb->prefix . "product_order";
$sql = "CREATE TABLE $table_name (
id mediumint(9) NOT NULL AUTO_INCREMENT,
range VARCHAR(255),
category VARCHAR(255),
order mediumint(9),
relation mediumint(9),
UNIQUE KEY id (id)
);";
$wpdb->query($sql);
That doesn't seem to create a table... any reasons why? I tried dbdelta too.
For the sake of an answer, surround the column names in back ticks as range and order are reserved words in MySQL:
$sql = "CREATE TABLE $table_name (
`id` mediumint(9) NOT NULL AUTO_INCREMENT,
`range` VARCHAR(255),
`category` VARCHAR(255),
`order` mediumint(9),
`relation` mediumint(9),
UNIQUE KEY id (id)
);";
http://dev.mysql.com/doc/refman/5.5/en/reserved-words.html
As a side note, if you want to make sure you don't conflict with other tables should this schema be shared with other wordpress instances or become a multisite, consider using {$wpdb->prefix}$table_name instead of just $table_name to use the database prefix defined in wp-config.php.