I'm having trouble converting this now() + INTERVAL
INSERT statement to a PDO prepared statement with named placeholders.
When I bind the value using either 'now() + INTERVAL 3 DAY'
or 'DATE_ADD(now(), INTERVAL 3 DAY)'
, it inserts 000's instead of the correct datetime (0000-00-00 00:00:00)
This is what I was previously using:
$qry = "INSERT INTO password_reset(user_id, temp_password, expiry_date)
VALUES('$member_user_id','$temp_password', now() + INTERVAL 3 DAY)";
New PDO Statement:
$stmt = $conn->prepare('INSERT INTO password_reset (user_id, temp_password, expiry_date)
VALUES(:user_id, :temp_password, :expiry_date)');
$stmt->bindValue(':user_id', $member_user_id);
$stmt->bindValue(':temp_password', $random_password);
$stmt->bindValue(':expiry_date', 'now() + INTERVAL 3 DAY');
$insertResult = $stmt->execute();
I've also tried this:
$stmt->bindValue(':expiry_date', 'DATE_ADD(now(), INTERVAL 3 DAY)');
Alternate method proposed in several SO postings
Several SO postings (including this link) suggested putting the now() statement in the VALUES instead of binding it, but that causes an error message 'Invalid parameter number: number of bound variables does not match number of tokens'
$stmt = $conn->prepare('INSERT INTO password_reset (user_id, temp_password, expiry_date)
VALUES(:user_id, :temp_password, :now() + INTERVAL 3 DAY)');
$stmt->bindValue(':user_id', $member_user_id);
$stmt->bindValue(':temp_password', $random_password);
$insertResult = $stmt->execute();