avatar
create database table by plugin WordPress

• This script is to create a database table named "flagtick_locations" if it doesn't exist, insert predefined data into the table, and flush the rewrite rules.

$sql = "CREATE TABLE flagtick_locations (
   `id` bigint NOT NULL AUTO_INCREMENT,
   `name` varchar(20) DEFAULT NULL,
   `address` varchar(255) NOT NULL,
   `longitude` varchar(255) NOT NULL,
   `latitude` varchar(20) DEFAULT NULL,
   `is_status` varchar(50) DEFAULT NULL,
   `is_editable` tinyint NOT NULL DEFAULT '0',
   `full_text` text,
   `created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
   `updated_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
   PRIMARY KEY (`id`)
)";

global $wpdb;

$createTable = $wpdb->prepare($sql);
$wpdb->query($createTable);

• You can use INSERT command to update sample data after create table successfully.

$addData = $wpdb->prepare("INSERT INTO flagtick_locations
                                   (name, address) VALUES
                                   ('First Location', '123 Main Street'),
                                   ('Second Location', '456 Main Street')");
$wpdb->query($addData);
You need to login to do this manipulation!