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 3 of 3
  1. #1
    Regular Coder
    Join Date
    Dec 2006
    Location
    In the wilderness
    Posts
    106
    Thanks
    9
    Thanked 5 Times in 5 Posts

    Question Storing a users date of birth in mysql DATE field question

    Hi I am creating a registration form for my site that contains three fields for the date of birth which I want to store in a mysql database. I would like to append the entire date of birth values to a single date field in mysql (YYYY-MM-DD format) so I can do date calculations such as age. How could I do this or would it better to store each field value in a separate database table column. Heres my code for my form so far.

    PHP Code:
    <form>
    <!-- previous elements -->
    <select name="dobd">
    <option value="">Day</option>
    <?php
    for($i 1$i <=31$i++)
    {
        echo 
    '<option value="'.$i.'">'.date('d'mktime(0,0,0,0,$i,0)).'</option>\n';
    }
    ?>
    </select>
    <select name="dobm">
    <option value="">Month</option>
    <?php
    for($i 1$i <= 12$i++)
    {
        echo 
    '<option value="'.$i.'">'.date('F'mktime(0,0,0,$i+1,0,0)).'</option>\n';
    }
    ?>
    </select>
    <input type="text" name="doby" size="4" maxlength="4" />
    <!-- later elements -->
    </form>
    Also as you can see I'm trying to create the days and month options using for loops. But my problem is I would like the form to remember these values on user error. Ive tried entering:

    PHP Code:
    ($_POST['dobd'] == $i) ? 'selected="selected"' '' 
    The for loop is a coding time-saver however putting that conditional in the loop does not work. Sorry I'm a PHP novice. Thanks in advance.
    "The advantage of computers is that they do exactly what you tell them to do. The disadvantage of computers, on the other hand, is that they do exactly what you tell them to do."

    Excellent resource for learning PHP here

  • #2
    Regular Coder
    Join Date
    Apr 2009
    Location
    somewhere over the rainbow
    Posts
    105
    Thanks
    2
    Thanked 14 Times in 14 Posts
    1. you should save day, month and year to unix timepost.
    when it's a one field unix timepost you can play with that as much as you want.
    http://php.net/manual/en/function.date.php and mkdir ..

    2. the same as you did
    PHP Code:
    <select name="dobm"> 
    <option value="">Month</option> 
    <?php 
    for($i 1$i <= 12$i++) 

      if (
    $_POST['dobd'] == $i)
        echo 
    '<option selected="selected" value="'.$i.'">'.date('F'mktime(0,0,0,$i+1,0,0)).'</option>\n'
      else
        echo 
    '<option value="'.$i.'">'.date('F'mktime(0,0,0,$i+1,0,0)).'</option>\n'

    ?> 
    </select>
    :. -= | Eran | =- .:
    __________________

  • Users who have thanked Eran for this post:

    karlosio (10-11-2009)

  • #3
    Regular Coder
    Join Date
    Dec 2006
    Location
    In the wilderness
    Posts
    106
    Thanks
    9
    Thanked 5 Times in 5 Posts
    Quote Originally Posted by Eran View Post
    1. you should save day, month and year to unix timepost.
    when it's a one field unix timepost you can play with that as much as you want.
    http://php.net/manual/en/function.date.php and mkdir ..

    2. the same as you did
    PHP Code:
    <select name="dobm"> 
    <option value="">Month</option> 
    <?php 
    for($i 1$i <= 12$i++) 

      if (
    $_POST['dobd'] == $i)
        echo 
    '<option selected="selected" value="'.$i.'">'.date('F'mktime(0,0,0,$i+1,0,0)).'</option>\n'
      else
        echo 
    '<option value="'.$i.'">'.date('F'mktime(0,0,0,$i+1,0,0)).'</option>\n'

    ?> 
    </select>
    Thank you!
    "The advantage of computers is that they do exactly what you tell them to do. The disadvantage of computers, on the other hand, is that they do exactly what you tell them to do."

    Excellent resource for learning PHP here


  •  

    Posting Permissions

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