Jump to content

Inserting date


kiwisi

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
https://forums.phpfreaks.com/topic/285422-inserting-date/
Share on other sites

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
https://forums.phpfreaks.com/topic/285422-inserting-date/#findComment-1465473
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.