Solved

WordPress: Adding Table on Plugin Activation / dbDelta

Posted on 2013-01-29
6
537 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Learn by example how to specify CSS selectors for Selenium WebDriver test automation software.
An enjoyable and seamless user experience can go a long way on an eCommerce site. While a cohesive layout and engaging copy play roles in creating a positive user experience, some sites neglect aspects that seem marginal but in actuality prove very …
The purpose of this video is to demonstrate how to integrate Mailchimp with WordPress, by placing a Mailchimp signup form on a WordPress Page or Post. This will be demonstrated using a Windows 8 PC. Mailchimp will be used. Log into your Mailchi…
The purpose of this video is to demonstrate how to set up an RSS Feed on a WordPress Website. This will be demonstrated using a Windows 8 PC. Feedburner will be used for this demonstration. Go to your WordPress login page. This will look like the…

920 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

16 Experts available now in Live!

Get 1:1 Help Now