大家好,我一整天都在尝试在数据库中添加一个表,但似乎找不到错误,我研究了多个类似的问题,但我的语法与所有问题都相似,在添加dbDelta()时我遵循了法典。
<?php
if( !function_exists(\'transaction_draft_save\') ){
//1, 234, donation, monday june 5 2018, give monthly, 10, usd, incomplete
function transaction_draft_save($user_id, $full_name, $email,
$invoiceNumber, $plan_type, $date, $membership_type, $plan_price, $currency,
$status){
global $taylormemorial_options;
global $wpdb;
$dbprefix = \'\';
$dbprefix = $wpdb->prefix;
$table_name = $wpdb->prefix ."user_transactions";
require_once( ABSPATH . \'wp-admin/includes/upgrade.php\' );
$sql = "CREATE TABLE IF NOT EXISTS " . $table_name . " (
\'id\' INT(6) NOT NULL AUTO_INCREMENT ,
\'user_id\' TEXT NOT NULL,
\'full_name\' TEXT NOT NULL,
\'email\' TEXT NOT NULL,
\'invoiceNumber\' TEXT NOT NULL,
\'plan_type\' TEXT NOT NULL,
\'date\' TEXT NOT NULL,
\'membership_type\' TEXT NOT NULL,
\'plan_price\' INT NOT NULL,
\'currency\' TEXT NOT NULL,
\'payment_method\' TEXT NOT NULL,
\'status\' TEXT NOT NULL,
PRIMARY KEY (id)
)";
dbDelta( $sql );
}
}
?>
另一个注意事项是,我在另一个页面中调用此函数来传递变量。如果我返回$sql variable和var\\u dump函数,它会很好地显示查询,因此范围不是问题所在。我检查了一次、两次、三次,但似乎找不到问题的根源,如果有人能提供帮助,我会非常感激并感谢您。
最合适的回答,由SO网友:Shibi 整理而成
您有语法错误。列名不需要单引号。
您可以使用phpmyadmin测试MySQL查询。
$sql = "CREATE TABLE IF NOT EXISTS $table_name (
id INT(6) NOT NULL AUTO_INCREMENT ,
user_id TEXT NOT NULL,
full_name TEXT NOT NULL,
email TEXT NOT NULL,
invoiceNumber TEXT NOT NULL,
plan_type TEXT NOT NULL,
date TEXT NOT NULL,
membership_type TEXT NOT NULL,
plan_price INT NOT NULL,
currency TEXT NOT NULL,
payment_method TEXT NOT NULL,
status TEXT NOT NULL,
PRIMARY KEY (id)
)";