Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating database table when installing a wordpress plugin

I want to create a table into the database upon installing the plugin I've created.

In my main plugin file (index.php):

register_activation_hook(__FILE__, 'wnm_install');

global $wnm_db_version;
$wnm_db_version = "1.0";

function wnm_install(){
global $wpdb;
global $wnm_db_version;
$sql = "CREATE TABLE tbl_campaigns (
campaignID int(11) NOT NULL AUTO_INCREMENT,
campaign_name varchar(128) NOT NULL,
start_duration date NOT NULL,
end_duration date NOT NULL,
activity varchar(500) NOT NULL,
survey_settings varchar(50) NOT NULL,
limit varchar(50) NOT NULL,
goal varchar(100) DEFAULT NULL,
PRIMARY KEY (campaignID)
) ;";

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

I just followed the instructions from this http://codex.wordpress.org/Creating_Tables_with_Plugins

But it doesn't work.

What seems to be the problem with this code?

like image 205
Jetoox Avatar asked Jun 09 '26 04:06

Jetoox


1 Answers

limit varchar(50) NOT NULL,

Limit is a keyword, change to something else like

`limit` varchar(50) NOT NULL,

Use back ticks around keywords

like image 142
Avyakt Avatar answered Jun 11 '26 17:06

Avyakt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!