Need to Add Date of Birth to Mysql

Discussion in 'PHP' started by digitalpointnet, Mar 22, 2011.

  1. #1
    I am going to have three combo boxes (DD-MM-YYYY) to obtain the date of birth of a user who is going to register in my site. My problem is how should i store this in mysql database?
     
    digitalpointnet, Mar 22, 2011 IP
  2. joy1986joy

    joy1986joy Member

    Messages:
    189
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    30
    #2
    try
    Insert into table_name (DD,MM,YY) values (12,03,1985)
    OR

    
    $dd='12';
    $mm='03';
    $yy='1986';
    echo $DB=$dd.$mm.$yy;
    //now save the $DB
    
    PHP:
     
    Last edited: Mar 22, 2011
    joy1986joy, Mar 22, 2011 IP
  3. ap2010

    ap2010 Guest

    Messages:
    41
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Ideally you should create ONE field (datatype=date) in your database to store date of birth. How you present this field on the form is up to you. Some website provide javascript popup calendar when they expect the user to enter a date. However, usually this is not the case with date of birth.

    If you are providing three list boxes, there are a couple of gotchas. E.g. user could specify a date in the future. To prevent this (and to disallow registrations for people below 13 years) truncate the year combo to 1998. Checking dates is tricky since not all months have 31 days. Then there are leap years. I'd recommend that you use the PHP checkdate() function to validate date:

    <?php
    $mm = (int) $_POST["dob_mm"];
    $dd = (int) $_POST["dob_dd"];
    $yy = (int) $_POST["dob_yy"];
    if (checkdate($mm, $dd, $yy)){
      $dob = "$yy-$mm-$dd"; // <- this is the format mysql expects for dates
      $query = "INSERT INTO whatever (x, y, z, dob) values ('x', 'y', 'z', '$dob')";
    }
    else {
      // reject user input
    }
    ?>
    PHP:
     
    ap2010, Mar 22, 2011 IP
  4. vediovis

    vediovis Peon

    Messages:
    35
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    concaten and insert
     
    vediovis, Mar 22, 2011 IP