Solved

WordPress: Adding Table on Plugin Activation / dbDelta

Posted on 2013-01-29
6
542 Views
Last Modified: 2013-02-04
I really could use some help. I'm starting to lose my mind.

I wrote a plugin a ways back, which worked in the past. On install / activation it is supposed to create a table in the database. I've installed the plugin on a new site and I cannot for the life of me get it to create a table like it had before.

I replaced my code with the sample code from the Codex and it works. I just can't see how mine differs.

I've tried to replace what's in the sample code with my own fields and data types and I'm back to square one. The plugin won't write the table.

I know plenty of people have had issues because of dbDelta and how particular it is about formatting. I've tried to follow people's advice for making mine conform to those rules, but to no avail.

Please help me see where I'm going wrong.

global $abc_reviews_db_version;
$abc_reviews_db_version = "1.0";

function abc_reviews_install() {
   global $wpdb;
   global $abc_reviews_db_version;

   $table_name = $wpdb->prefix . "abc_reviews";

$sql = "CREATE TABLE " . $table_name . " (
          id mediumint(9) NOT NULL AUTO_INCREMENT,
          review_date DATE(50) NOT NULL,
          review_author VARCHAR(250) NOT NULL,
          text text NOT NULL,
          UNIQUE KEY id (id)
        );";

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

  add_option("abc_reviews_db_version", $abc_reviews_db_version);
}

register_activation_hook(__FILE__,'abc_reviews_install');

Open in new window

0
Comment
Question by:michaelsmith1994
  • 4
  • 2
6 Comments
 
LVL 70

Accepted Solution

by:
Jason C. Levine earned 500 total points
ID: 38833829
The plugin won't write the table.

Have you tried turning debug mode on and then installing and running the plugin?
0
 

Author Comment

by:michaelsmith1994
ID: 38833885
I didn't know such an option existed.  Thanks for letting me know.

This is what I got back:

WordPress database error: [You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(50) NOT NULL, review_author VARCHAR(250) NOT NULL, text tex' at line 3]
CREATE TABLE wp_pcg_reviews ( id mediumint(9) NOT NULL AUTO_INCREMENT, review_date DATE(50) NOT NULL, review_author VARCHAR(250) NOT NULL, text text NOT NULL, UNIQUE KEY id (id) )

Do you see the problem?
0
 
LVL 70

Expert Comment

by:Jason C. Levine
ID: 38834054
'text' is a reserved word in MySQL.  Maybe change that to something else and try it.  It might not be a dbDelta issue and instead just a plain old MySQL issue.

(I totally know your frustration.  I had a similar issue with dbDelta once too).
0
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 

Author Comment

by:michaelsmith1994
ID: 38835150
I've been changing the code frequently to try and find a way around this issue.
 
Originally that field name was "review_text" and it didn't work.  But just to be thorough, I changed it back and re-uploaded the plugin.  

Unfortunately, I get the same error:

WordPress database error: [You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(50) NOT NULL, review_author VARCHAR(250) NOT NULL, review_t' at line 3]
CREATE TABLE wp_pcg_reviews ( id mediumint(9) NOT NULL AUTO_INCREMENT, review_date DATE(50) NOT NULL, review_author VARCHAR(250) NOT NULL, review_text text NOT NULL, UNIQUE KEY id (id) )

0
 

Author Comment

by:michaelsmith1994
ID: 38851724
My SQL code was bad news bears.

I think I was misusing date() by passing it a character limit.

My inexperience was at fault.  Thanks for the help.
0
 

Author Closing Comment

by:michaelsmith1994
ID: 38851725
This got me on the right path.  Thanks.
0

Featured Post

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Envision that you are chipping away at another e-business site with a team of pundit developers and designers. Everything seems, by all accounts, to be going easily.
Because your company can’t afford for you to make SEO mistakes, you’ll want to ensure you’re taking the right steps each and every time you post a new piece of content. This list of optimization do’s and don’ts can help you become an SEO wizard.
The purpose of this video is to demonstrate how to update a WordPress Site’s version. WordPress releases new versions of its software frequently and it is important to update frequently in order to keep your site secure, and to get new WordPress…
The purpose of this video is to demonstrate how to set up basic WordPress SEO. This will be demonstrated using a Windows 8 PC. The plugin used will be WordPress SEO by Yoast. Go to your WordPress login page. This will look like the following: myw…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question