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

  • SEARCH
  • Home
  • 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 8204863
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T08:01:39+00:00 2026-06-07T08:01:39+00:00

I am trying to use prepared statements to insert a datetime for a library

  • 0

I am trying to use prepared statements to insert a datetime for a library application. Here is the code thus far:

    global $dbh;
    $query = "INSERT INTO `loan` SET
             `title` = (?), //example value - Lord of the Rings
             `description` = (?), //example value - Trilogy
             `start_date` = (?), //example value 20120701 in String datatype
             `end_date` = (?)";  //example value 20120702 in String datatype

    $statement = $dbh->prepare($query);
    $statement->bind_param("ssss", $title,$description,$startDate,$endDate);

    $statement->execute();
    print $statement->error; //to check errors
    $statement->close();

However, I cannot seem to insert this value into the row. At the same time, somehow the

            print $statement->error 

does not seem to display any error.

Any help will really do.

UPDATE:

It actually works. I was just referencing the wrong database. But I want to add a little outro for new people who chanced upon this.

  1. Remove all the comments as mentioned in the comments/answers as they will mess up your string.

  2. For DATETIME, remember to specify the datatype as String as MySQL only recognises String datatype. If you are not using prepared queries, this means you have to add ” quotes for the values.

  3. The insertion will result in the date (2012-07-01 00:00:00) format as time is not specified.

  4. Both SQL queries work. INSERT INTO tbl_name SET col_name = value or INSERT INTO tbl_name(col_name) VALUES (value) work.

  • 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-07T08:01:44+00:00Added an answer on June 7, 2026 at 8:01 am

    Try something like this:

    global $dbh;
    $query = "INSERT INTO loan (title, description, start_date, end_date) VALUES (?,?,?,?)"
    
    $statement = $dbh->prepare($query);
    $statement->bind_param("ssss", $title,$description,$startDate,$endDate);
    
    $statement->execute();
    print $statement->error; //to check errors
    $statement->close();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to harden some of my PHP code and use mysqli prepared statements
I am trying to use prepared statements which operate on a database located quite
I'm new to PHP and PDO, and I try to use prepared statements here.
I am trying out prepared statements, but the below code is not working. I
I'm trying to use prepared statements in a PHP script that accesses an SQLite3
I have to upgrade the following code to use prepared statements: OdbcCommand cmd =
I am trying to use staticsan´s answer in this question for prepared statements. Lets
I'm trying to use prepared statements to set a table name to select data
I'm trying to convert a site to use prepared mysql statements, however I'm having
I'm trying to use prepared statements in PostgreSQL, but it's giving me some pretty

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.