这是一个代码审查问题,而不是WordPress问题。请在Code Review Stack Exchange 未来
根据最佳实践,您有两件事做错了或不正确。我所做更改的内联注释。
$cr_table_array = array(
array(
\'data_field\' => \'id\',
// Add primary key here to allow you to keep definitions in one place.
\'data_type\' => \'MEDIUMINT(12) NOT NULL AUTO_INCREMENT PRIMARY KEY\',
),
array(
\'data_field\' => \'your_name\',
\'data_type\' => \'VARCHAR(200) NOT NULL\',
),
array(
// misspelled
\'data_field\' => \'your_email\',
\'data_type\' => \'VARCHAR(200) NOT NULL\',
),
);
global $wpdb;
// Use .= to append your statement pieces into a single variable.
// Use curly braces for inline vaiables.
$sql = "CREATE TABLE IF NOT EXISTS {$wpdb->prefix}credofy_contact_form( ";
foreach ( $cr_table_array as $file ) {
$sql .= $file[\'data_field\'] . \' \' . $file[\'data_type\'] . \', \';
}
// Remove the final comma to prevent sql errors.
$sql = rtrim( $sql, \', \' );
$sql .= \')\';
// This will create your database. You can stop here.
$wpdb->query( $sql );
global $wpdb;
$statement = "CREATE TABLE IF NOT EXISTS {$wpdb->prefix}credofy_contact_form( ";
foreach ( $cr_table_array as $file ) {
$statement .= $file[\'data_field\'] . \' \' . $file[\'data_type\'] . \', \';
}
$statement = rtrim( $statement, \', \' );
$statement .= \' )\';
$wpdb->query( $statement );