Insert CURRENT_TIMESTAMP with prepared statement

只愿长相守 提交于 2019-12-02 11:43:37

问题


I´m inserting data with prepared statements. How can I assign a variable to CURRENT_TIMESTAMP and bind the value?

I have a database table with the columns "name, lastfed, lastdoc and id". The 2 columns called lastfed and lastdoc are of type timestamp. I have a function and once I run it I want lastfed and lastdoc to register the current date and time.

$lastfed = CURRENT_TIMESTAMP;
$lastdoc = CURRENT_TIMESTAMP;

$query = $db->prepare("INSERT INTO test (`name`, `lastfed`, `lastdoc`, `id`) VALUES (?, ?, ?, ?)");

$query->bindValue(1, $name);
$query->bindValue(2, $lastfed);
$query->bindValue(3, $lastdoc);
$query->bindValue(4, $user);

回答1:


INSERT INTO test (`name`, `lastfed`, `lastdoc`, `id`) 
VALUES (?, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, ?)

Or you set CURRENT_TIMESTAMP as default to these columns DB wise. Then you can leave them from your insert statement:

INSERT INTO test (`name`, `id`) 
VALUES (?, ?)


来源:https://stackoverflow.com/questions/20245596/insert-current-timestamp-with-prepared-statement

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!