0

I'm trying to build a pluging, in the php file I have tried the following code to create a table:

<?php
    function createTable(){
    global $wpdb;

   $table_name = 'tblresults';

   if($wpdb->get_var("SHOW TABLES LIKE '$table_name'" ) != $table_name){
   $sql= "CREATE TABLE $table_name (
       id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, 
       firstname VARCHAR(30) NOT NULL,
       lastname VARCHAR(30) NOT NULL,
       email VARCHAR(50),
       reg_date TIMESTAMP,
       CH int(2) NOT NULL,
       DI int(2) NOT NULL,
       RE int(2) NOT NULL,
       CR int(2) NOT NULL,
       A  int(2) NOT NULL,
       ME int(2) NOT NULL,
       CO int(2) NOT NULL,
       CN int(2) NOT NULL
       );";
       require_once (ABSPATH. 'wp-admin/includes/upgrade.php' );
       dbDelta($sql);
      }
     }

   createTable();
?>

but each time I try I get a blank page with a 500 code error. Thanks in advance for any help!

1
  • Your code syntax is correct, but it has to be tested in a right context. It does look to me that you run it as a standalone script on your website. Put the script in /wp-content/mu-plugins then it will create your table. Oct 2 at 1:25

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.