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

database - How to create a table

programmeradmin7浏览0评论
Closed. This question needs details or clarity. It is not currently accepting answers.

Want to improve this question? Add details and clarify the problem by editing this post.

Closed 4 years ago.

Improve this question

This code doesn't work for me. Do you have one where I can create a table in my database?

function creation() {
  
  global $wpdb;

    $table_name = $wpdb->prefix . 'form';
 
    $sql = "CREATE TABLE $table_name (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `name` varchar(255) NOT NULL,
      `email` varchar(255) NOT NULL,
      UNIQUE KEY id (id)
    );";
   
    require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
   
}

add_action('wp', 'creation');

}
Closed. This question needs details or clarity. It is not currently accepting answers.

Want to improve this question? Add details and clarify the problem by editing this post.

Closed 4 years ago.

Improve this question

This code doesn't work for me. Do you have one where I can create a table in my database?

function creation() {
  
  global $wpdb;

    $table_name = $wpdb->prefix . 'form';
 
    $sql = "CREATE TABLE $table_name (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `name` varchar(255) NOT NULL,
      `email` varchar(255) NOT NULL,
      UNIQUE KEY id (id)
    );";
   
    require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
   
}

add_action('wp', 'creation');

}
Share Improve this question edited Jul 22, 2020 at 6:35 fuxia 107k38 gold badges255 silver badges459 bronze badges asked Jul 22, 2020 at 4:50 JavierJavier 9 1
  • 1 Your code doesn't do anything. Please see the example at codex.wordpress/Creating_Tables_with_Plugins, and read thoroughly. You have left out several important lines and your SQL does not follow the format described. – Jacob Peattie Commented Jul 22, 2020 at 4:58
Add a comment  | 

1 Answer 1

Reset to default 1

Here you go Javier, this should work. I included comments within the code to try and show you where things needed to change and added a few things you were missing. I'll explain more below.

<?php
/* Should really come up with something more unique and related to your plugin */
function yourplugin_dbcreation() {
    global $wpdb;
    $charset_collate        = $wpdb->get_charset_collate();
    /* Please come up with something more descriptive or unique for this */
    $table_name             = $wpdb->prefix . 'form';
    require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
    /* Since you're not only running this on activation, let's do a quick check to see if it exists before we move further */
    if( $wpdb->get_var( "SHOW TABLES LIKE '$table_name'" ) != $table_name ) {
        /* dbDelta() has strict rules about how to format the SQL statement, field types must be lower case, do not put headings in backticks, etc. */
        $sql = "CREATE TABLE $table_name (
            id int(11) NOT NULL AUTO_INCREMENT,
            name varchar(255) NOT NULL,
            email varchar(255) NOT NULL,
            UNIQUE KEY id  (id),
            PRIMARY KEY  (id)
        );";
        dbDelta( $sql );
        /* You add this next line in case you ever want to update the table structure, so you'd run a version check to determine whether or not to execute the change.  If your new version matches this option, you do nothing, if the option is lower than your new table version, you'd run the code for the update. */
        add_option( 'form_db', '1.0' );
    }
}
/* you could use this, but better to run it on activation with register_activation_hook( 'plugin_path', 'your_function' );*/
add_action( 'init', 'yourplugin_dbcreation' );
?>

So a couple of things, as Jacob Peattie points out in his comment, there are details you needed to address and lines of code you needed to include that are explained in the link he provided.

dbDelta() is a very specific and finicky thing - so you really have to make sure you observe the rules. It's not lenient at all and if you don't follow it's strict instructions it just won't execute.

You were also missing a few key lines, like dbDelta() itself - without that, WP doesn't know what to do with your code.

Personally I prefer creating the table on plugin activation, but you didn't provide any plugin information and you have to get the path absolutely correct or it won't work. (Jacob's link actually shows you how to do it.)

Final thing, you've got to be a bit more unique and descriptive when naming functions and tables. Even if you don't personally need them to be descriptive, at least ensure you put some effort into them being unique so that they're not generic like creation or form. When you leave them generic like that, all you're doing is inviting conflicts when you install something by another developer who also didn't make their code unique. Then you suddenly have two functions named creation and neither one works. If you consider the amount of tutorials and snippets out there that people are using, it's not at all uncommon.

Hope this all helps. :-)

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论