Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

<?php
/*
Template Name:Register Page
Template URI:
Author:yuva
Author URI:
Description:This Is Page for Gallery.
*/

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

<?php
global $wpdb;
$my_table=$wpdb->prefix.'your_users';

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

include_once(ABSPATH.'wp-admin/includes/upgrade.php');

dbDelta($sql);

?>
</div>
<?php
get_footer();
?>
share|improve this question
    
print the return value from dbDelta. It shows what has been done. –  KDM Jan 6 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. –  KDM Jan 6 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 at 17:42

1 Answer 1

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:

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

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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