0

I'm not sure why this isn't running. When I output dbDelta it says that it created the table, but it doesn't.

I think my SQL is valid. Taking everything out between id and UNIQUE KEY results in the table being made, but I don't see the problem with the other lines.

Thanks

function email_install () {
   global $wpdb;

   $table_name = $wpdb->prefix . "table_name_email"; 

   $charset_collate = $wpdb->get_charset_collate();

   $sql = "CREATE TABLE " . $table_name . " (
     id INT NOT NULL AUTO_INCREMENT,
     time DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL,
     email VARCHAR NOT NULL,
     page VARCHAR NOT NULL,
     UNIQUE KEY  (id)
   ) ". $charset_collate . ";";

   require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
   dbDelta( $sql );

}

1 Answer 1

4

There is error in sql query. You have defined time as datetime and default value mentioned in current_timestamp. Define time as timestamp. Also specify length of email and page fields.

Corrected query:

$sql = "CREATE TABLE " . $table_name . " (
     id INT NOT NULL AUTO_INCREMENT,
     time TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL, //Change datetime to timestamp
     email VARCHAR(50) NOT NULL, //Change length
     page VARCHAR(50) NOT NULL, //Change length
     UNIQUE KEY  (id)
   ) ". $charset_collate . ";";

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.