Solved

WordPress: Adding Table on Plugin Activation / dbDelta

Posted on 2013-01-29
6
531 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
The purpose of this video is to demonstrate how to prevent comment spam on a WordPress Website. This will be demonstrated using a Windows 8 PC. Plugin Akismet will be used. Go to your WordPress login page. This will look like the following: myw…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

708 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now