Well, I don’t know if that’s exactly what I understood. Suppose you have the following table:
CREATE TABLE `test`.`tasks` (
`id` INT NOT NULL AUTO_INCREMENT,
`created_at` DATETIME NULL,
PRIMARY KEY (`id`)
);
To insert a log with the current server date, the following command is sufficient:
INSERT INTO tasks (created_at) VALUES (now());
mysql> select * from tasks;
+----+---------------------+
| id | created_at |
+----+---------------------+
| 1 | 2016-01-19 11:49:58 |
+----+---------------------+
1 Row in set (0.00 sec)
Using PHP (with PDO), just do the following:
<?php
// algo mais ou menos como: 2016-01-19 11:56:30
$data_actual = date('Y-m-d H:i:s');
$sql = "INSET INTO tasks(created_at) VALUES (:created_at)";
$stmt = $pdo->prepare($sql);
$stmt->bindParam(':created_at', $data_actual);
$stmt->execute();
UPDATE:
Note: By error, you are trying to add a type value datetime in a field of the kind date
UPDATE 2:
By table structure, the field in name date is or may cause problems.
Change the name of this field. Yes date is used as a type of Mysql data (http://dev.mysql.com/doc/refman/5.7/en/date-and-time-types.html).
After you do this update, you can do the Insert this way:
INSERT INTO tasks (created_at) VALUES (now());
and if before you set date_default_timezone_set('America/Sao_paulo'); what happens?
– Bia
Have you ever tried to define your field like this?
date datetime NOT NULL DEFAULT NOW(),– Marco Souza
What version of your Mysql ? Try using
Current_Date.– Diego Souza
I found this post: http://optimize-this.blogspot.com.br/2012/04/datetime-default-now-finally-available.html. What we usually do is leave the type field
timestampand then use thecurrent_timestampornow()as default.– Diego Souza
@Marconciliosouza
ERROR 1067: Invalid default value for 'date'– Jorge B.
@Zoom is
MySQL 5.1.73. But I wanted it to bedatetime.– Jorge B.
@Jorgeb. shows the structure of your table
– WeezHard
@psantos is done.
– Jorge B.
@Jorgeb. first hint: change the field name
datefordataormy_date.dateis a type for Mysql soon there will be confusion– WeezHard
@you’re right it was just an experiment. And I figured out the problem, I’m using
mysqli_and was using 'i' as parameter type and has to be’s'.– Jorge B.