kiwisi Posted January 16, 2014 Share Posted January 16, 2014 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? Quote Link to comment https://forums.phpfreaks.com/topic/285422-inserting-date/ Share on other sites More sharing options...
Solution Barand Posted January 16, 2014 Solution Share Posted January 16, 2014 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, Quote Link to comment https://forums.phpfreaks.com/topic/285422-inserting-date/#findComment-1465473 Share on other sites More sharing options...
kiwisi Posted January 16, 2014 Author Share Posted January 16, 2014 Beauty - thanks mate. Thats cleared it up. Quote Link to comment https://forums.phpfreaks.com/topic/285422-inserting-date/#findComment-1465503 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.