最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

mysql - Is it possible to create a WordPress table using array and loop?

programmeradmin0浏览0评论

I am trying to get all data fields name and data types from an array, but the query is not working.

<?php
$cr_table_array = array(
array(
'data_field' => "id",
'data_type' => "mediumint(12) NOT NULL AUTO_INCREMENT"
),
array(
'data_field' => "your_name",
'data_type' => "varchar(200) NOT NULL"
),
array(
'data_field' => "your_eamil",
'data_type' => "varchar(200) NOT NULL"
)
);


global $wpdb;
$sql =  "CREATE TABLE IF NOT EXISTS " . $wpdb->prefix.'credofy_contact_form'. " (
foreach($cr_table_array as $file) {
    echo $file['data_field'] .' '. $file['data_type'].' '
    }
             PRIMARY KEY (id));";
$wpdb->query($sql);
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
echo $sql;
?>

I am trying to get all data fields name and data types from an array, but the query is not working.

<?php
$cr_table_array = array(
array(
'data_field' => "id",
'data_type' => "mediumint(12) NOT NULL AUTO_INCREMENT"
),
array(
'data_field' => "your_name",
'data_type' => "varchar(200) NOT NULL"
),
array(
'data_field' => "your_eamil",
'data_type' => "varchar(200) NOT NULL"
)
);


global $wpdb;
$sql =  "CREATE TABLE IF NOT EXISTS " . $wpdb->prefix.'credofy_contact_form'. " (
foreach($cr_table_array as $file) {
    echo $file['data_field'] .' '. $file['data_type'].' '
    }
             PRIMARY KEY (id));";
$wpdb->query($sql);
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
echo $sql;
?>
Share Improve this question edited Apr 22, 2019 at 10:02 fuxia 107k39 gold badges255 silver badges459 bronze badges asked Apr 22, 2019 at 9:50 gaurav mishragaurav mishra 94 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

This is a code review question more than a WordPress question. Please post these in the Code Review Stack Exchange in the future.

You have a couple things wrong or not according to best practices. Comments inline for where I made changes.

$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 );
发布评论

评论列表(0)

  1. 暂无评论