Forums

Create table on activation problem (2 posts)

  1. rgtyf
    Member
    Posted 3 years ago #

    Hello to everyone,

    I want my plugin to create a table when it is activated. Could anyone, please, tell me what I am doing wrong here?

    MY CODE:

    function TagRank_Activate()
    {
    $tags_ratings = $wpdb->prefix . "tags_ratings";

    if ($wpdb->get_var("SHOW tables LIKE '$tags_ratings'") != $tags_ratings)
    {
    $sql = "CREATE table" . $tags_ratings . "(
    id mediumint(9) NOT NULL AUTO_INCREMENT,
    post_id mediumint(9) NOT NULL,
    tag_id mediumint(9) NOT NULL,
    rating bigint(11) DEFAULT '0' NOT NULL,
    UNIQUE KEY id (id)
    );";
    require_once(ABSPATH . 'wp-admin/upgrade-functions.php');
    dbDelta($sql);
    }
    }

    add_action('activate_tagrank.php', 'TagRank_Activate');

    My plugin is stored at wp-content/plugins/tagrank.php.

    Thank you very much.

  2. johnmorris09
    Member
    Posted 2 years ago #

    I know it's old here, but thought I would reply, since this is the number one listing for "create table problem" and I didn't see the solution posted anywhere else.

    This code doesn't work, because you need backticks around your table name and table columns in your sql query. I can't even post the correct code here, because the editor keeps stripping them out no matter what I do. I would guess that's why the solution is so elusive... no one can get it to post. :)

    Anyway, backticks are the little apostrophe-looking things all the way to the left hand side of your keyboard on the numbers row... so, it goes...

    3... 2... 1... backtick...

    Not the squiggly one, which is a tilde... just the apostrophe-looking one. That's it...

    Put those around the quotations around tablename and around the column ids... i.e. id, post_id, etc... and you'll be good to go.

    I banged my head against the wall for a day trying to figure this out... it wasn't till I stumbled onto an obscure video that the author mentioned this.

    I'm not sure why it's not mentioned in the codex, but it probably should be.

    Thanks!!
    John

Topic Closed

This topic has been closed to new replies.

About this Topic