Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Databases

James Barrett
James Barrett
13,253 Points

How do you insert the date and time of 'NOW' into the database?

Hi there,

I am trying to store social media posts into a MySQLi database:

PHP:

  if(!isset($error_message)) {
    $postStore = $db->query("INSERT INTO `Post`(P_Body, P_Dateadded, User_U_ID)     VALUES ('{$post}', 'NOW()', '{$_SESSION['U_ID']}' )");
  }

The datatype for P_Dateadded is DATETIME(); however the 'NOW()' function is not working. Moreover, I have tried using unix_timestamp() and date() and still nothing works.

Other INSERT values are being stored successfully.

Is it my syntax?

Thanks, James.

1 Answer

It looks like your problem is the way you are writing the function. Here's an update I just did in one of my databases:

UPDATE Activites SET DateEntered = now() WHERE ActivityID = 5441

Note that now() is not in quotes, nor is it capitalized.