$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>plugin development - Creating two tables in database on activation hook|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

plugin development - Creating two tables in database on activation hook

matteradmin9PV0评论

I have been trying to generate two tables in database on activation hook. But it generates only the first one table on activation hook . What am i doing wrong in my code?

class Datetimepicker_Tables{

function __construct(){
    add_action('init',array($this,'create_tables'));

}

function activate(){
    $this->create_tables();
    flush_rewrite_rules();
}

function create_tables(){
        global $wpdb;

        $date_table_name = $wpdb->prefix . 'booking_dates';
        $time_table_name = $wpdb->prefix . 'booking_timeslots';
        $charset_collate = $wpdb->get_charset_collate();

        $date_table = "CREATE TABLE $date_table_name (
            `id` int(11) NOT NULL AUTO_INCREMENT,
             `year` int(11) NOT NULL,
             `month` int(11) NOT NULL,
             `day` int(11) NOT NULL,
             PRIMARY KEY (`id`)
            ) $charset_collate;";

        $time_table = "CREATE TABLE $time_table_name (
            `id` int(11) NOT NULL AUTO_INCREMENT,
            `bid` int(11) NOT NULL,
            `time` int(11) NOT NULL,
            PRIMARY KEY (`id`),
            KEY `bid` (`bid`),
            CONSTRAINT `booking_timeslots_ibfk_1` FOREIGN KEY (`bid`) REFERENCES `booking_dates` (`id`)
            )  $charset_collate;";  

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

$tablesclass = new Datetimepicker_Tables();
register_activation_hook(__FILE__,array($tablesclass,'activate'));

I have been trying to generate two tables in database on activation hook. But it generates only the first one table on activation hook . What am i doing wrong in my code?

class Datetimepicker_Tables{

function __construct(){
    add_action('init',array($this,'create_tables'));

}

function activate(){
    $this->create_tables();
    flush_rewrite_rules();
}

function create_tables(){
        global $wpdb;

        $date_table_name = $wpdb->prefix . 'booking_dates';
        $time_table_name = $wpdb->prefix . 'booking_timeslots';
        $charset_collate = $wpdb->get_charset_collate();

        $date_table = "CREATE TABLE $date_table_name (
            `id` int(11) NOT NULL AUTO_INCREMENT,
             `year` int(11) NOT NULL,
             `month` int(11) NOT NULL,
             `day` int(11) NOT NULL,
             PRIMARY KEY (`id`)
            ) $charset_collate;";

        $time_table = "CREATE TABLE $time_table_name (
            `id` int(11) NOT NULL AUTO_INCREMENT,
            `bid` int(11) NOT NULL,
            `time` int(11) NOT NULL,
            PRIMARY KEY (`id`),
            KEY `bid` (`bid`),
            CONSTRAINT `booking_timeslots_ibfk_1` FOREIGN KEY (`bid`) REFERENCES `booking_dates` (`id`)
            )  $charset_collate;";  

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

$tablesclass = new Datetimepicker_Tables();
register_activation_hook(__FILE__,array($tablesclass,'activate'));
Share Improve this question asked Mar 6, 2019 at 11:39 Afzal KhanAfzal Khan 54 bronze badges 5
  • REFERENCES booking_dates should be REFERENCES $date_table_name. – Sally CJ Commented Mar 6, 2019 at 12:23
  • Are you talking about this -> "$date_table_name = $wpdb->prefix . 'booking_dates';" ? – Afzal Khan Commented Mar 6, 2019 at 12:27
  • Yes, the table name. – Sally CJ Commented Mar 6, 2019 at 12:33
  • I don't think so, i made a variable and passing a name of the table in that variable. – Afzal Khan Commented Mar 6, 2019 at 12:39
  • I was saying that you didn't use the proper table name in the CONSTRAINT clause.. – Sally CJ Commented Mar 6, 2019 at 13:00
Add a comment  | 

1 Answer 1

Reset to default 1

You're not using the proper table name in your CONSTRAINT clause:

CONSTRAINT `booking_timeslots_ibfk_1` FOREIGN KEY (`bid`) REFERENCES `booking_dates` (`id`)

That should be:

CONSTRAINT `booking_timeslots_ibfk_1` FOREIGN KEY (`bid`) REFERENCES `$date_table_name` (`id`)

where $date_table_name (as I could see) is the correct table name.

Post a comment

comment list (0)

  1. No comments so far