Results 1 to 3 of 3

Thread: Inserting dates into a database posted from html form.

Hybrid View

  1. #1
    Join Date
    May 2013

    Inserting dates into a database posted from html form.

    Hi team!

    I am feeding data into a database using a html form with date text input field. I don't want to enter the current date as what I am recording happened in the previous but I still need to calculate the number of days, months and years since the event occured. I have writen the following code to insert date into the database table:

    $insert_date=maysql_query("insert into table_name(date_field)VALUES('$event_date')";
    surpringly, the insert query is inserting 0000-00-00. The user is entering date in m-d-yyyy format. I have validated the user input date using the following

    $date_arr = explode('-', $event_date);
    if (checkdate($date_arr[0], $date_arr[1], $date_arr[2])==false) {
    // valid date ...
    $errors[]="What you entered is not a valid date format";
    the validation have worked fine.

    This is my question: What is going wrong in that the query is inserting 0000-00-00 instead of the date value? When echo the $event_date before inserting into
    the database is like is working fine. Any help please.thank you in advance.

  2. #2
    Join Date
    Mar 2011
    The Pleiades
    $insert_date=maysql_query("insert into table_name(date_field)VALUES('$event_date')"; -- Not sure if that is causing any problems, possible though.

    Also, look where your closing double quote is for your query..... - it's outside of the mysql_query() function.

    Also, what are your users using to enter the date? A basic text input?

    I would use 3 select inputs with the required number of options for each select element. Give your users the least possible chance of entering something obscure. Forcing them to select from a list will prevent them entering bad data.

    What type of field is your database date field - I'm guessing DATETIME?

    Kind regards,


  3. #3
    Join Date
    Aug 2004
    PHP Code:
    foreach(array('5-8-2003''5/8/2003') as $input) {
    $time strtotime($input);
    $input.": time = $time, date = ".date('Y-m-d'$time)."<br />\n";
    5-8-2003: time = 1060041600, date = 2003-08-05
    5/8/2003: time = 1052352000, date = 2003-05-08
    Which is why I prefer to have 3 separate input fields for dates.
    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center