在激活wp插件时无法创建多个自定义数据库表

时间:2015-04-27 07:44:58

标签: php mysql wordpress

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

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

4 个答案:

答案 0 :(得分:5)

您正在覆盖$sql变量。 只需在每次查询后使用dbDelta函数。 喜欢以下格式。

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

答案 1 :(得分:1)

它会发生,因为在你的函数中,你总是在执行它们之前重写$sql变量。这就是为什么只执行最后一次。

在每个$sql定义之后,运行查询。

//define the query
$sql = "CREATE TABLE `firsttable` ....";
//Run the query
dbDelta($sql); //If this is what is running the query.
//Another table
$sql = "CREATE TABLE `secondtable` ....";
//Run the new query
dbDelta($sql); //If this is what is running the query.

答案 2 :(得分:0)

如果dbDelta($sql);运行你的sql,你必须在每个sql语句中添加这行以执行它

答案 3 :(得分:0)

你也可以尝试这样的事情:

用于第一次查询wright $sql = "your query text goes here"

以及所有后续查询$sql .= "your query text goes here"