问题
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