在WordPress数据库站点中创建表时会出现500错误

时间:2018-06-08 10:24:51

标签: mysql database wordpress plugins

我正在学习如何为WordPress创建插件。我想在WordPress DB中创建一个表。但在编写用于开发表站点的代码后显示500错误。我尝试使用MySQL Workbench创建一个表,但该网站无法正常工作。我尝试在其他函数中执行此操作并初始化插件激活但它没有工作。这是一个用于创建表的代码:

function my_plugin_start(){

    $status_url = get_option('');
    preg_match('/^http(s)?\/\/[^\/]+\/(.*)$/i', $status_url, $matches);

    $real_url = $_SERVER['REQUEST_URI'];
    preg_match('/^\/(.+)(\?.+)$/i', $real_url, $uri_matches);

    if ($uri_matches[1] == $matches[2]){
        if(isset($_GET['dcode'])){
            start_download();
        }
        else {
            interkassa_process();
        }
    }

// The code for creating a table

    global $wpdb;

    $table_name = $wpdb->prefix . "my_product_base";

    if($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name) {
        $sql = "CREATE TABLE " . $table_name . " (
      UNIQUE KEY id (id) AUTO_INCREMENT,
      name VARCHAR(255) NOT NULL,
      cost VARCHAR(255) NOT NULL,
      url VARCHAR(255) NOT NULL

    );";

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

    }
}

add_action('admin_menu', 'my_plugin_add_admin_menu');
add_action('init', 'my_plugin_start');

1 个答案:

答案 0 :(得分:0)

试用此修改后的代码

function my_plugin_start(){

    $status_url = get_option('');
    preg_match('/^http(s)?\/\/[^\/]+\/(.*)$/i', $status_url, $matches);

    $real_url = $_SERVER['REQUEST_URI'];
    preg_match('/^\/(.+)(\?.+)$/i', $real_url, $uri_matches);

    if ($uri_matches[1] == $matches[2]){
        if(isset($_GET['dcode'])){
            start_download();
        }
        else {
            interkassa_process();
        }
    }

// The code for creating a table

    global $wpdb;

    $table_name = $wpdb->prefix . "my_product_base";

    if($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name) {
        $sql = "CREATE TABLE " . $table_name . " (
      UNIQUE KEY id (id) AUTO_INCREMENT,
      name VARCHAR(255) NOT NULL,
      cost VARCHAR(255) NOT NULL,
      url VARCHAR(255) NOT NULL

    );";

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

    }
}

add_action('admin_menu', 'my_plugin_add_admin_menu');
add_action('init', 'my_plugin_start');