Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Senior Coder
    Join Date
    May 2004
    Posts
    1,466
    Thanks
    15
    Thanked 0 Times in 0 Posts

    insert date from form to db

    I have a form taht takes a date with 3 fields

    mymonth,myday,myyear

    How would I combine these to insert them to a mysql datetimefield -- waht's the correct way to combine them?

  • #2
    Regular Coder Element's Avatar
    Join Date
    Jul 2004
    Location
    Lynnwood, Washington, US
    Posts
    855
    Thanks
    2
    Thanked 2 Times in 2 Posts
    well there is plenty of correct ways to do it, but to add it to a datetimefieeld or w/e i think it has to bee numeral. But to combine them it could simply be done like:

    have the thre date fields with the name="date[]" and then do something like:

    PHP Code:


    // 1
    $date '';
    foreach(
    $_POST['date'] as $value) {
      
    $date .= $value ' ';
    }
    $date trim($date);

    // 2
    $date $_POST['date'][0] . ' ' $_POST['date'][1] . ' ' $_POST['date'][2]; 

  • #3
    Regular Coder
    Join Date
    Jun 2005
    Posts
    804
    Thanks
    0
    Thanked 0 Times in 0 Posts
    MySQL accepts a number of variations of the basic 'YYYYMMDD' format, as far as using delimiters and such. You can use any punctuation as a delimiter, but not spaces -- 'YYYY-MM-DD' or 'YYYY&MM&DD' will work, but 'YYYY MM DD' will not. Delimiters aren't necessary, though, and the db will store them the same way regardless, so there's not much point to using them. You might as well just concatenate your date into one 'YYYYMMDD' string.

  • #4
    Senior Coder
    Join Date
    May 2004
    Posts
    1,466
    Thanks
    15
    Thanked 0 Times in 0 Posts
    if i do this

    $date = '';
    foreach($_POST['date'] as $value) {
    $date .= $value . ' ';
    }
    $date = trim($date);

    // 2
    $date = $_POST['date'][0] . ' ' . $_POST['date'][1] . ' ' . $_POST['date'][2];


    do i ahve to put the select boxes in any specific order?

  • #5
    Senior Coder
    Join Date
    Mar 2003
    Location
    Atlanta
    Posts
    1,037
    Thanks
    14
    Thanked 30 Times in 28 Posts
    Quote Originally Posted by esthera
    I have a form taht takes a date with 3 fields

    mymonth,myday,myyear
    If you have them in th order you describe you can just change the last line of code around from this.
    PHP Code:
    $date $_POST['date'][0] . ' ' $_POST['date'][1] . ' ' $_POST['date']; 
    to be like this
    PHP Code:
    $date $_POST['date'][2] . '-' $_POST['date'][0] . '-' $_POST['date'][1]; 
    The key is in naming all of the select boxes the same name..... name = date[]
    And like Kid Charming said, you can use any or no delimiter except for space.(i.e. ' ').
    Last edited by StupidRalph; 02-15-2006 at 07:46 AM.
    Most of my questions/posts are fairly straightforward and simple. I post long verbose messages in an attempt to be thorough.


  •  

    Posting Permissions

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