www.webdeveloper.com
Results 1 to 3 of 3

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

Hybrid View

  1. #1
    Join Date
    May 2013
    Posts
    119

    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:

    $event_date=mysql_real_escape_string($_POST['date']);
    $event_date=date('Y-m-d,strtotime($event_date));
    $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

    code:
    $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
    Location
    The Pleiades
    Posts
    31
    $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,

    Lc.

  3. #3
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,322
    PHP Code:
    <?php
    foreach(array('5-8-2003''5/8/2003') as $input) {
        
    $time strtotime($input);
        echo 
    $input.": time = $time, date = ".date('Y-m-d'$time)."<br />\n";
    }
    Output:
    Code:
    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.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

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



Recent Articles