Luke Villeneuve
Luke Villeneuve

Reputation: 41

Cannot save date: WordPress database error: [Column 'date' cannot be null]

I trying to save some data from a simple form, into the database. So far all is well, a simple name and the email address, however I want to save the date in which the data was save into the database, but for some reason it's not working, I am getting this error, just as the title suggests:

WordPress database error: [Column 'date' cannot be null]

Here's the code from the form:

if(isset($_POST['submit_ebook_btn'])) {

        global $wpdb;
        
        $tablename = $wpdb->prefix.'ebook_users';
        $name = $_POST['ebook_name'];
        $email = $_POST['ebook_email'];

        $insertDb = $wpdb->insert( $tablename, array(
          'name' => $name,
          'email' => $email,
          'date' => the_time('Ymd')
        ),
        array(
          '%s', 
          '%s', 
          '%s' 
        ));

     if($insertDb) { ?>
      <div>
        <h2>Download the ebook <a href="#">here</a></h2>
      </div>
    <?php } else {
      echo 'Something went wrong! Please try again.';
    }
}
  1. Also, how would I redirect to a success/thank you page when the page is submitted? Sorry for the newbie question, fairly new with php.

Upvotes: 0

Views: 290

Answers (1)

MasterBlends
MasterBlends

Reputation: 11

The current time won't be returned without specifying a null value or else providing a specific time-zone such as $gmt.

function wp_date( $format, $timestamp = null, $timezone = null ) { global $wp_locale;

if ( null === $timestamp ) {
    $timestamp = time();
} elseif ( ! is_numeric( $timestamp ) ) {
    return false;
}

if ( ! $timezone ) {

Upvotes: 1

Related Questions