激活wp插件时无法创建多个自定义数据库表 [英] Not able to create multiple custom db tables on activation of wp Plugin

查看:156
本文介绍了激活wp插件时无法创建多个自定义数据库表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

激活我的wordpress插件后,我无法在db中创建多个自定义表,它仅创建此代码中提到的最后一个表为书签",而不是创建所有表(用户,字段,可见性,通知)等

i am unable to create multiple custom tables in db on activation of my wordpress plugin it creates only the last table like mentioned in this code is "Bookmark" instead of creating all tables (user,field,visibility,notify) etc

function your_plugin_options_install() {
    global $wpdb;
    //global $your_db_name;
    $uservar = $wpdb->prefix . 'user';
    $fieldvar = $wpdb->prefix . 'field';
    $visibilitytypevar = $wpdb->prefix . 'visibily';
    $notificationvar = $wpdb->prefix . 'notification';
    $jobtypevar = $wpdb->prefix . 'jobtype';
    $bookmarkvar = $wpdb->prefix . 'bookmark';
   // $profiledatavar = $wpdb->prefix . 'profiledata';
    //$employeevar = $wpdb->prefix . 'employee';
 //echo "some";
    // create the ECPT metabox database table
    //if($wpdb->get_var("SHOW TABLES LIKE '$yourtable'") != $yourtable) 
    //{
        $sql = "CREATE TABLE IF NOT EXISTS " . $uservar . " (
        `u_id` int(20) NOT NULL AUTO_INCREMENT,
        `u_name` varchar(30) ,
        `u_phonenumber` int(20) ,
        `u_email` varchar(30) NOT NULL,
        `u_password` varchar(50) NOT NULL,
         PRIMARY KEY (u_id)
         );";
          $sql = "CREATE TABLE IF NOT EXISTS " . $fieldvar . " (
         `f_id` int(20) NOT NULL AUTO_INCREMENT ,
         `f_title` varchar(30) NOT NULL,
          PRIMARY KEY (f_id)
         );";

         $sql = "CREATE TABLE IF NOT EXISTS " . $visibilitytypevar . " (
         `v_id` int(20) NOT NULL AUTO_INCREMENT ,
         `v_type` bool NOT NULL,
          PRIMARY KEY (v_id)
         );";

         $sql = "CREATE TABLE IF NOT EXISTS " . $notificationvar . " (
         `n_id` int(20) NOT NULL AUTO_INCREMENT ,
         `u_id` int(20) NOT NULL,
         PRIMARY KEY (n_id)
         );";

         $sql = "CREATE TABLE IF NOT EXISTS " . $jobtypevar . " (
         `jt_id` int(20) NOT NULL AUTO_INCREMENT ,
         `jt_title` varchar(30),
         PRIMARY KEY (jt_id)
         );";

          $sql = "CREATE TABLE IF NOT EXISTS " . $bookmarkvar . " (
         `bm_id` int(20) NOT NULL AUTO_INCREMENT ,
         `u_id` int(20) NOT NULL,
         `bm_title` varchar(30),
         PRIMARY KEY (bm_id)
         );";


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

//}
// run the install scripts upon plugin activation
register_activation_hook(__FILE__,'your_plugin_options_install');

推荐答案

您正在覆盖您的$sql变量. 每次查询后只需使用dbDelta函数. 像下面的格式.

you are overriding your $sql variable. Just use dbDelta function after every query. like below format.

require_once(ABSPATH . 'wp-admin/includes/upgrade.php');

// your first query
$sql = '...'; 
dbDelta($sql);

// your second query
$sql = '...'; 
dbDelta($sql);

.
.
.


// your nth query
$sql = '...'; 
dbDelta($sql);

这篇关于激活wp插件时无法创建多个自定义数据库表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆