Jump to content

Inserting date


kiwisi
Go to solution Solved by Barand,

Recommended Posts

as a total noob - I'm having trouble inserting dates using php and myswl.

 

Basically, I want to have the current date and time auto inserted when a new record is added to my table.

 

I've formatted the date field (articledate) to TIMESTAMP (or should it be datetime...)

 

The following code is retrieved from another table so am just wondering how to alter the insert for the date...

$query = "insert into gadgets

set

articletitle = '".$mysqli->real_escape_string($_POST['articletitle'])."',

articledescription = '".$mysqli->real_escape_string($_POST['articledescription'])."',

articledate = '".$mysqli->real_escape_string($_POST['articledate'])."',

myimages  = '".$mysqli->real_escape_string($_POST['myimages'])."'";

Clear as mud?

 

 

Link to comment
Share on other sites

  • Solution

TIMESTAMP will automatically record the date added. Did you specify the default?

$sql =  "CREATE TABLE date_sample (
    id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
    date_added TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    name VARCHAR(25)
)";

#$db->query($sql);

$sql = "INSERT INTO date_sample (name) VALUES
('Peter'),('Paul'),('Mary')";

$db->query($sql);

mysql> SELECT * FROM date_sample;
+----+---------------------+-------+
| id | date_added          | name  |
+----+---------------------+-------+
|  1 | 2014-01-16 20:57:22 | Peter |
|  2 | 2014-01-16 20:57:22 | Paul  |
|  3 | 2014-01-16 20:57:22 | Mary  |
+----+---------------------+-------+

If you want to record the datetime of updates also, use

DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.