2012-03-31 36 views
3

プラグイン用にwordpressでカスタムテーブルを作成する必要があります。私はいくつかのオンラインテキストをたどってテーブルを作成しましたが、アクセスできないことがわかりました。テーブルからすべてを選択しようとすると、何も返されず、データベースブラウザープラグインを使用してテーブルを表示しようとすると、次のエラーが表示されます。「SQL構文に誤りがあります。プラグインのクエリ( "SELECT SQL_CALC_FOUND_ROWS FROM wp-typeEvents LIMIT 0、100 ;;")に応答して、 'FROM wp-typeEvents LIMIT 0,100'(1行目)の近くで使用する正しい構文のバージョン。dbDelta関数を使用してWordpressでテーブルを作成する

要するに、私はdbDeltaを使ってテーブルを作成しようとしています。表が作成されますが、行を追加できない、または内容を読み取ることができない問題があります。

私はdbDeltaはfinnicky関数であることを読んだので、私はその3つの黄金ルールに固執しようとしました:

主キーの間に2つのスペースを-Putting新しい行
の各フィールドを-Putting

global $wpdb; 

$tablename = "wp-typeEvents"; 
$query = "CREATE TABLE `" . $tablename . "` (
    `id` mediumint(9) NOT NULL AUTO_INCREMENT, 
    `eventName` varchar(60) NOT NULL, 
    `location` varchar(60) DEFAULT '' NULL, 
    `price` double NOT NULL, 
    `description` text NOT NULL, 
    `paypal` varchar(60) NOT NULL, 
    PRIMARY KEY (`id`) 
    );"; 

require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); 
dbDelta($query); 

の任意のアイデア:と少なくとも1つのキー

を-Havingその定義
は、ここでのコードですか?

+0

私自身の問題を解明しました。この関数は、テーブル名のハイフンで窒息していました。 – Fabulinus

答えて

0

自分自身の問題と同様に、テーブルプレフィックスをハードコーディングするように見えます。あなたはこれをしてはいけません。マニュアルの代わりに、次のものを使用する必要があります。 -

$wpdb->prefix 

これはプレフィックスを格納し、テーブル名の前に付ける必要があります。これにより、プレフィックスが変更される可能性があります。

http://codex.wordpress.org/Creating_Tables_with_Plugins

1

それはプラグイン内にある場合、require_onceを()のパスが間違っています。必要があります:

require_once('/includes/upgrade.php'); 

dbDelta()関数をロードできるように修正する必要があります。

これが役に立ちます。

+2

'require_once(ABSPATH。 'wp-admin/includes/upgrade.php');は正しいです。 – ninty9notout

+0

プラグイン機能は、プラグインがロードされるときに** wp-admin **ディレクトリから呼び出されるため、パス全体を書き込む必要はありません。 –

+0

@ ninty9notout:今私はあなたのことを理解しています:それは問題の道です。私は答えたときにそれがそうだったとは思わないが、私は覚えていない。とにかく注意してくれてありがとう。 –

3

テーブル名をハードコードしないで、$ wpdb-> prefixを使用してください。

フィールド名の前後に引用符を使用しないでください。

は、同様に他の有用な「ルール」があり、それらはすべてここに記載されています: はhttp://codex.wordpress.org/Creating_Tables_with_Plugins

Note that the dbDelta function is rather picky, however. For instance:

  • 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.
  • Field types must be all lowercase.
  • SQL keywords, like CREATE TABLE and UPDATE, must be uppercase.
0

また、現在dbDelta()機能が注文キーワードASCを解析していないことがわかり取る| UNIQUE KEYにDESCを参照している列の名前。残りのインデックスに制約を追加するため、一意キーエラーを引き起こす制約を追加しようとします。以下の例を参照してください。

このクエストを作成しようとすると、エラーが発生します。

CREATE TABLE wp_test (
     id int(11) NOT NULL AUTO_INCREMENT, 
     email varchar(100) NOT NULL, 
     PRIMARY KEY (stcr_id), 
     UNIQUE KEY uk_email (subscriber_email ASC)) 
ENGINE = InnoDB 
DEFAULT CHARACTER SET utf8; 

デフォルトでは、注文タイプはASCなので、それを取り除くと機能します。

CREATE TABLE wp_test (
     id int(11) NOT NULL AUTO_INCREMENT, 
     email varchar(100) NOT NULL, 
     PRIMARY KEY (stcr_id), 
     UNIQUE KEY uk_email (subscriber_email)) 
ENGINE = InnoDB 
DEFAULT CHARACTER SET utf8; 

私はワードプレスのdbDeltaのコア機能を使用しながら、いくつかの問題に直面し、そのための関数を作成することを決定したのWordPress 4.1.1

0

でこれをテストした:

/** 
* Prevents unnecessary re-creating index and repetitive altering table operations when using WordPress dbDelta function 
* 
* Usage Example: 
* 
* $table_name  = "ratings"; 
* 
* $table_columns = "id INT(6) UNSIGNED AUTO_INCREMENT, 
*     rate tinyint(1) NOT NULL, 
*     ticket_id bigint(20) NOT NULL, 
*     response_id bigint(20) NOT NULL, 
*     created_at TIMESTAMP"; 
* 
* $table_keys  = "PRIMARY KEY (id), 
*     KEY ratings_rate (rate), 
*     UNIQUE KEY ratings_response_id (response_id)"; 
* 
* create_table($table_name, $table_columns, $table_keys); 
* 
* Things that need to be considered when using dbDelta function : 
* 
* 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. 
* Field types must be all lowercase. 
* SQL keywords, like CREATE TABLE and UPDATE, must be uppercase. 
* You must specify the length of all fields that accept a length parameter. int(11), for example. 
* 
* Further information can be found on here: 
* 
* http://codex.wordpress.org/Creating_Tables_with_Plugins 
* 
* @param $table_name 
* @param $table_columns 
* @param null $table_keys 
* @param null $charset_collate 
* @version 1.0.1 
* @author Ugur Mirza Zeyrek 
*/ 
function create_table($table_name, $table_columns, $table_keys = null, $db_prefix = true, $charset_collate = null) { 
    global $wpdb; 

    if($charset_collate == null) 
     $charset_collate = $wpdb->get_charset_collate(); 
    $table_name = ($db_prefix) ? $wpdb->prefix.$table_name : $table_name; 
    $table_columns = strtolower($table_columns); 

    if($table_keys) 
     $table_keys = ", $table_keys"; 

    $table_structure = "($table_columns $table_keys)"; 

    $search_array = array(); 
    $replace_array = array(); 

    $search_array[] = "`"; 
    $replace_array[] = ""; 

    $table_structure = str_replace($search_array,$replace_array,$table_structure); 

    $sql = "CREATE TABLE $table_name $table_structure $charset_collate;"; 

    // Rather than executing an SQL query directly, we'll use the dbDelta function in wp-admin/includes/upgrade.php (we'll have to load this file, as it is not loaded by default) 
    require_once (ABSPATH . 'wp-admin/includes/upgrade.php'); 

    // The dbDelta function examines the current table structure, compares it to the desired table structure, and either adds or modifies the table as necessary 
    return dbDelta($sql); 
} 

https://github.com/mirzazeyrek/wordpress_create_table

関連する問題