简体   繁体   中英

PHP MySQL database not creating new table

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM