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

标签 mysql database wordpress plugins

我正在学习如何创建 WordPress 插件。我想在 WordPress 数据库中创建一个表。但是在编写开发表站点的代码后显示 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');

最佳答案

尝试修改后的代码

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');

关于mysql - 在 WordPress 数据库站点中创建表时出现 500 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50758553/

相关文章:

php - MySQL - 连接失败 : No route to host

mysql - EMS MySQL 管理器迁移

database - QlikView 中的非标准连接?

SQL:指定列的冗余 WHERE 子句 > 0?

database - 有没有办法使用 sqflite 检查 flutter 应用程序中的数据库版本?

css - Wordpress 主题开发 - WP 标准 CSS 类未知

swift - 我如何在 WordPress REST API(版本 2)中创建评论

MYSQL 如何对两列任意组合(双向)创建UNIQUE约束

mysql - 如何将相同的列值分组并映射到具有相应帖子值的新列

php - 显示来自 concat mysql 和 php 的图像