0

I am trying to redesign my previous code into a plugin but I have run into some issues. When I try to create a table using the below code. The table is not created. I tried to debug but I get no errors :S

Appreciate if you can have a look at the code. I also have another question. After I create the table I need to populate it via a csv file. Do you guys have any idea how to go around this as I have no idea where to start.

Thanks again

function upper_table(){
global $wpdb;
$table_name = $wpdb->prefix . "upper_winds";
if($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name){
$sql = "CREATE TABLE $table_name ( 
`LVL` VARCHAR(4) NOT NULL, 
`REGION` TEXT NOT NULL, 
`VALID` INT NOT NULL, 
`CURRENT` TEXT NOT NULL 
);";
//reference to upgrade.php file
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
dbDelta( $sql );

} }

This code is in a functions php file and is called from the main plugin via

include ("functions_wx.php");
register_activation_hook(__FILE__,'upper_table');

The sql query above has been passed through PHPmyAdmin directly and has worked fined. Do not know if it could be a problem but I am currently running through WAMP server.

F117a
  • 31
  • 1
  • 4

1 Answers1

0

Try replacing

 $sql = "CREATE TABLE $table_name ( 
`LVL` VARCHAR(4) NOT NULL, 
`REGION` TEXT NOT NULL, 
`VALID` INT NOT NULL, 
`CURRENT` TEXT NOT NULL 
);";

With

 $sql = "CREATE TABLE ".$table_name." ( 
`LVL` VARCHAR(4) NOT NULL, 
`REGION` TEXT NOT NULL, 
`VALID` INT NOT NULL, 
`CURRENT` TEXT NOT NULL 
);";

(Notice the quotes and dots around the table name)

If that doesn't work, you can debug by var dumping the variable $table_name to ensure it contains a valid string:

var_dump($table_name);

If it does, then try storing the return value of the dbDelta() function in a variable instead of just calling the function, like this: $result = dbDelta( $sql );

Then var_dump the result like this: var_dump($result);

That way you can begin to understand where at in your code the problem resides, and whether its an issue with your syntax/variables or the dbDelta() function you're using.

Brock Amhurst
  • 497
  • 2
  • 5