Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9203987
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T23:35:59+00:00 2026-06-17T23:35:59+00:00

I’m having trouble converting this now() + INTERVAL INSERT statement to a PDO prepared

  • 0

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(); 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T23:36:00+00:00Added an answer on June 17, 2026 at 11:36 pm

    Remove the colon from :now() + INTERVAL 3 DAY.

    $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(); 
    

    Alternatively, you could do this:

    $stmt = $conn->prepare('
        INSERT INTO
            password_reset (user_id, temp_password, expiry_date) 
        VALUES
            (:user_id, :temp_password, now() + INTERVAL 3 DAY)
    ');
    $insertResult = $stmt->execute(array(
        ":user_id" => $member_user_id,
        ":temp_password" => $random_password)
    );
    

    When working with Prepared Statements, you should think of the statement as being a template to plug data into.

    If you have a database table with the structure:

    user_id           INT
    temp_password     VARCHAR(128)
    expiry_date       DATE
    

    You have three columns in your table. Two of the values to insert into these columns will come from PHP, and the other will be evaluated by SQL. Since SQL will be doing all the work on the expiry_date column, it doesn’t need to be bound to anything in PHP.

    So, examining the INSERT statement:

    INSERT INTO password_reset (user_id, temp_password, expiry_date) VALUES (:user_id, :temp_password, now() + INTERVAL 3 DAY)
    

    Each parameter of the VALUES() clause needs to match up to a declared column name in the INSERT INTO table (columns...) clause.

    In prepared statements, since we are creating a template, we use placeholders to show where we will be inserting values into the statement as it is executed. Placeholders can be either the :name version that you have used, or simply a question mark ?.

    PDO::prepare($statement) tells the SQL server to prepare the statement. At that point, SQL has the template for your query, and is ready to receive the values for the placeholders in that query.

    PDOStatement::execute($placeholderValues) executes a single instance of that prepared statement, substituting the placeholders with the values you have either bound with bindParam, bindValue, or passed as the argument to execute().

    So, basically, all that is being sent to the SQL server on each execute() are the values to plug into the placeholders, instead of an entire query string.

    Now comes the part that explains why you weren’t able to bindValue(":expiry_date", "now() + INTERVAL 3 DAY").

    When the values get to the SQL server, SQL server sanitizes them and replaces the respective placeholder with their value.

    When you bind "now() + INTERVAL 3 DAY", you are actually binding a string. Since it is a string, it doesn’t get executed as SQL code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I have this code to decode numeric html entities to the UTF8 equivalent character.
We're building an app, our first using Rails 3, and we're having to build
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
This could be a duplicate question, but I have no idea what search terms

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.