I want to create a new table in current database of word press through create a static page. I write code for this as below, but there was no result. Why is that, and how can I get this right?

Template Name:Register Page
Template URI:
Author URI:
Description:This Is Page for Gallery.

<div class="regist">
<h2><?php  the_title(); ?></h2>

global $wpdb;

$sql="CREATE TABLE IF NOT EXISTS $my_table (id int(20) not null auto_increment,name varchar(40) not null,password varchar(20) notnull,PRIMARY KEY(id))";



  • print the return value from dbDelta. It shows what has been done. – Dakshinamurthy Karra Jan 6 '14 at 10:53
  • print the return value from dbDelta. It shows what has been done. And also 'not null' misspelled for password field, I guess. – Dakshinamurthy Karra Jan 6 '14 at 10:59
  • Why are you creating a gallery database table, why not just create a gallery post type using the standard WordPress APIs and save the trouble of building an entire interface and archives? What does it say when you put that SQL into a mysql client? – Tom J Nowell Mar 8 '14 at 17:42

DBDelta is extremely picky-- to the point of being maddening sometimes. From the Codex:

  • 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.

It looks to me like you have violated at least two of those rules. You should have something more like this:

  CREATE TABLE $my_table (
    id int(20) not null auto_increment,
    name varchar(40) not null,
    password varchar(20) notnull,

I removed IF NOT EXISTS. dbdelta will update the table if the table needs updating and leave it alone otherwise, hence the name "database delta". In mathematics "delta"-- the Greek letter-- means something like "change" or "difference".

I do not promise that that code will work since, as I said, dbdelta is very picky. I am sure that it is the format of your SQL statement that is the problem and that should get you moving in the right direction.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.