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.
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
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);
I have set up four additional tables for my plugin to use what I am trying to do is take a name and assign it a ID then use this data to populate drop down menus with a name and the same for class and position I am unsure as to how to do this correctly this is what i have so far.
$sql = "CREATE TABLE $tableName (
recordID int NOT NULL AUTO_INCREMENT,
PRIMARY KEY(recordID),
driverID int,
driverName varchar(30),
classID int,
driverClass varchar(20),
posID,
driverPosition varchar(6),
trackName varchar(30),
raceDate date
);";
$sql = "CREATE TABLE $driverTableName (
driverID int NOT NULL AUTO_INCREMENT,
PRIMARY KEY(driverID),
driverName varchar(30)
);";
$sql = "CREATE TABLE $classTableName (
classID int NOT NULL AUTO_INCREMENT,
PRIMARY KEY(classID),
className varchar (20)
);";
$sql = "CREATE TABLE $posTableName (
posID int NOT NULL AUTO_INCREMENT,
PRIMARY KEY(posID),
posName varchar(6)
);";
The bottom three tables will store the data I want to populate the drop down boxes to create a record with I am unsure as to how to link them to the top table where this record will be stored.
This is pretty much a indexing issue. If you are going to access the database separately to the standard calls that Wordpress provides, you should at the very least use http://codex.wordpress.org/Class_Reference/wpdb as it will save you some coding time.
The rest of it is a MySQL question. (Assuming you are using MySQL) In how to properly index the data together and then parsing the data as it comes in.
Consider the following tables for a LMS:
Item (
ID BIGINT NOT NULL UNIQUE AUTO_INCREMENT,
ConnectLog BIGINT NOT NULL,
ItemClass BIGINT NOT NULL,
ItemType BIGINT NOT NULL,
AcqDate TIMESTAMP NOT NULL DEFAULT NOW(),
Vendor BIGINT NOT NULL,
Cost DECIMAL(64,2) NOT NULL DEFAULT '0.00',
Image VARCHAR(255),
Access INTEGER NOT NULL,
Notes VARCHAR(255),
PRIMARY KEY (ID)
)
Book (
ID BIGINT NOT NULL UNIQUE AUTO_INCREMENT,
Item BIGINT NOT NULL UNIQUE,
ISBN BIGINT,
Title VARCHAR(255) NOT NULL,
Authors VARCHAR(255),
Publisher VARCHAR(255),
DDC VARCHAR(255),
PubDate DATETIME,
Edition VARCHAR(255),
BookCase VARCHAR(255),
Shelf VARCHAR(255),
PRIMARY KEY (ID)
)
Now when a user makes an entry for Book, first an entry for Item has to be created first. But i need to find the ID for the Item entry that was created so i can use that value for Item in the Book table...
How? :/
Use mysql_insert_id()
// Create Entry
$sql = "INSERT INTO TABLE () VALUES()";
mysql_query($sql);
$id = mysql_insert_id();
// Create Book
$sql = "INSERT INTO TABLE (`Item_ID`) VALUES(".$id.")";
mysql_query($sql);
I bet there is a MySQL Command you could use to do it in a single query. But, this works.
According to this link, you could do an SQL query like:
INSERT INTO foo (auto,text) VALUES(NULL,'text'); # generate ID by inserting NULL
INSERT INTO foo2 (id,text) VALUES(LAST_INSERT_ID(),'text'); # use ID in second table
I haven't tested it, but it seems as though LAST_INSERT_ID() contains the last inserted ID, no matter what table it was inserted into.
If you're worried about heavy loads, and LAST_INSERT_ID() not containing the appropriate entry's ID, you could wrap these SQL statements in a transaction.