Solved

WordPress: Adding Table on Plugin Activation / dbDelta

Posted on 2013-01-29
6
564 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

 

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

When it comes to write a Context Sensitive Help (an online help that is obtained from a specific point in state of software to provide help with that state) ,  first we need to make the file that contains all topics, which are given exclusive IDs. …
There’s a good reason for why it’s called a homepage – it closely resembles that of a physical house and the only real difference is that it’s online. Your website’s homepage is where people come to visit you. It’s the family room of your website wh…
The purpose of this video is to demonstrate how to add AdSense Ads to a WordPress Website, and how to set up WordPress to automatically place Ads in Sidebars. This will be demonstrated using a Windows 8 PC. Log into your AdSense account. : Cli…
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…

705 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