0

I'm trying to create a new table in phpmyadmin. I'm using MAMP and this is of course inside wordpress.

<?php
function testtheme_database() {
  global $wpdb;

  global $testtheme_db_version;
  $testtheme_db_version = "1.0";

  $table = $wpdb->prefix . "inquiries";

  $charset_collate = $wpdb->get_charset_collate();


  $sql = "CREATE TABLE $table (
    id mediumint(9) NOT NULL AUTO_INCREMENT,
    first-name varchar(50) NOT NULL,
    last-name varchar(50) NOT NULL,
    email varchar(50) DEFAULT '' NOT NULL,
    message longtext NOT NULL,
    PRIMARY KEY (id)
  ) $charset_collate; ";

  require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
  dbDelta($sql);
}
add_action( 'after_setup_theme', 'testtheme_database' );

I'm getting this error when I try to refresh/reload a certain page in wordpress, in my case it's the contact us page:

WordPress database error: [You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '-name varchar(50) NOT NULL, last-name varchar(50) NOT NULL, email varcha' at line 3] CREATE TABLE wp_inquiries ( id mediumint(9) NOT NULL AUTO_INCREMENT, first-name varchar(50) NOT NULL, last-name varchar(50) NOT NULL, email varchar(50) DEFAULT '' NOT NULL, message longtext NOT NULL, PRIMARY KEY (id) ) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_520_ci

It should create me a new table inside phpmyadmin with the name wp_inquiries but just getting that error and no table created.

  • 1
    based on that error, I'll guess it has a problem with the hyphen in your column name – Milo Mar 5 at 4:23
1

From MySQL reference:

  • Permitted characters in unquoted identifiers:
    • ASCII: [0-9,a-z,A-Z$_] (basic Latin letters, digits 0-9, dollar, underscore)
    • Extended: U+0080 .. U+FFFF
  • Permitted characters in quoted identifiers include the full Unicode Basic Multilingual Plane (BMP), except U+0000:
    • ASCII: U+0001 .. U+007F
    • Extended: U+0080 .. U+FFFF
  • ASCII NUL (U+0000) and supplementary characters (U+10000 and higher) are not permitted in quoted or unquoted identifiers.
  • Identifiers may begin with a digit but unless quoted may not consist solely of digits.
  • Database, table, and column names cannot end with space characters.

So you have to remove - characters from the names of columns or quote these names.

It’s worth to mention that it would be a good idea to use the same naming conventions that WP is using. So table names should be lower-case and use _ as separator.

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.