Johannes Nyman Johannes Nyman - 4 months ago 15
MySQL Question

SQL: current timestamp + 1 hour when inserting into DB

Sorry if this question has been asked before. I've looked around and have only seen this being used when using SELECT, not INSERT and it has gotten me quite confused.

What I've been trying is:

$statement = $pdo->prepare("INSERT INTO shipments (crate_type_id, username, arrival, quantity, type, status) VALUES ('$crate_type_id', '$username', 'DATE_ADD(NOW() + INTERVAL 1 HOUR)', '$quantity', 'purchase', 'active')");


I don't get any errors, it only inserts
"0000-00-00 00:00:00"
. I've tried troubleshooting in phpmyadmin etc but haven't made any progress.

Answer

You should remove the quotes arount the date_add expression and use comma not +

statement = $pdo->prepare("INSERT INTO shipments 
  (crate_type_id, username, arrival, quantity, type, status) 
  VALUES ('$crate_type_id', '$username',
         DATE_ADD(NOW() ,  INTERVAL 1 HOUR) ,
           '$quantity', 'purchase', 'active')");
Comments