How do I enter birthdate into mysql database from php form

by ~kev~
14 replies
Guys and gals, I need some help - please. How do I get someones birthdate from a form, and enter it into a mysql database?

The form is built that gives the drop down menus, but the information is not going into the database. All of the other information is - name, address, phone number, zip code,,,,, everything besides the birthdate.
#birthdate #database #enter #form #mysql #php
  • Profile picture of the author KirkMcD
    Are you getting an error or are you just not including the field on the INSERT?
    {{ DiscussionBoard.errors[2552699].message }}
  • Profile picture of the author zeeshi570
    You can use strtotime function to store strings in time format and then can pass them to date function and format
    {{ DiscussionBoard.errors[2552737].message }}
    • Profile picture of the author ~kev~
      Originally Posted by zeeshi570 View Post

      You can use strtotime function to store strings in time format and then can pass them to date function and format
      Could you explain that in terms that I can understand? Maybe dumb it down a little bit?

      And, could you please provide an example?


      Originally Posted by KirkMcD View Post

      Are you getting an error or are you just not including the field on the INSERT?
      No error message.

      Everything looks like its included in the $sql = "INSERT INTO but nothing is going into the date fields of the database.
      {{ DiscussionBoard.errors[2552921].message }}
      • Profile picture of the author Tim Brownlaw
        G'day Kev

        Just a quick question to clarify what you've already got...

        You've got 3 different drop downs for Day, Month and Year?
        Are you trying to save them into a single field in the DB?

        If you can answer those ( if you haven't solved this already ) we can take it from there...

        Cheers
        Tim
        {{ DiscussionBoard.errors[2661284].message }}
        • Profile picture of the author ~kev~
          Originally Posted by Tim Brownlaw View Post

          You've got 3 different drop downs for Day, Month and Year?
          Are you trying to save them into a single field in the DB?
          Yes, I have 3 different drop down menus for selecting the date - year, month, day.

          Because I have not been able to find an answer, I have resorted to a single box where the data entry person has to enter the date manually. This is causing problems as they are not entering the date "exactly" as it needs to be.
          {{ DiscussionBoard.errors[2661396].message }}
  • Profile picture of the author SteveJohnson
    Depends on how your dropdowns are structured. If you're passing month names:
    Code:
    #date = date('Y-m-d', strtotime(#_POST['month'] . "-" . #_POST['day'] . "-" . #_POST['year']);
    If you have numbers assigned as values for the month dropdown, just combine the values into one variable and store it.
    Code:
    extract( #_POST );
    #birthdate = #year . "-" . #month . "-" . #day;
    This forum is mangling code. Substitute dollar sign for hash mark.
    Signature

    The 2nd Amendment, 1789 - The Original Homeland Security.

    Gun control means never having to say, "I missed you."

    {{ DiscussionBoard.errors[2662210].message }}
    • Profile picture of the author caesargus
      From what I have read on this topic so far, this appears to be the solution that I would use.

      If you are not sure where your error is in your sql statement, I suggest getting that issue corrected before trying to insert the date into the database. A single error in the sql statement can leave you scratching your head for hours if you're not sure how to debug the issue.

      Here is how I would work the problem:
      Take your sql statement and print it out on the screen (echo or print command allows you to do this) and then copy and paste the displayed sql (this should have all of your variables populated at this point) and drop it into your sql console (phpmyadmin or sql server management studio).

      The sql should execute correctly (if you have trouble finding the sql window in phpmyadmin, I can walk you through that separately). If your sql is not executing correctly when you place it in the sql console, start reworking the query until you are able to get an executable query.

      (Ok, it appears to have an issue with dates, so remove the date columns, and try and execute again, no there is an issue with the number of columns not matching the values sent, rip out extra value columns and try again, ... repeat the process until you can execute the sql.)

      Once you have found and fixed the basic issue (data is now appearing in the database, but there still is an issue with some of the data not making it into the database). I would then search on google for a solution to that problem - google: "How to insert dates into database mysql" - read over a couple of articles related to that solution, and try and solve the problem using the solutions discussed in the articles.

      These are basic debugging techniques that any developer uses to solve problems. Trust me, there have been times when I have been tripped up on this very same issue, and usually it's more related to the basic sql syntax than the actual technique of how I'm inserting the data. (Oh I forgot a comma there, STUPID! - and then pretty soon, everyone will wonder why you're yelling at your computer screen).

      I hope this information is helpful. If you want any more tips, let me know (send me a PM, because I might not check back to this post), I'd be happy to share.

      Originally Posted by SteveJohnson View Post

      Depends on how your dropdowns are structured. If you're passing month names:
      Code:
      #date = date('Y-m-d', strtotime(#_POST['month'] . "-" . #_POST['day'] . "-" . #_POST['year']);
      If you have numbers assigned as values for the month dropdown, just combine the values into one variable and store it.
      Code:
      extract( #_POST );
      #birthdate = #year . "-" . #month . "-" . #day;
      This forum is mangling code. Substitute dollar sign for hash mark.
      {{ DiscussionBoard.errors[2666372].message }}
  • Profile picture of the author icun
    This should be easy to fix. Can you post a link to your form and your HTML code and I'll have a look. Can't really see what exactly is wrong without seeing the code.
    {{ DiscussionBoard.errors[2662932].message }}
    • Profile picture of the author ~kev~
      Originally Posted by icun View Post

      This should be easy to fix. Can you post a link to your form and your HTML code and I'll have a look. Can't really see what exactly is wrong without seeing the code.
      Uh,,,, no - I can not post a link to the form. Its for a medical provider, and nobody from the outside world can gain access to it.

      I'am a hardware / networking guy, and management dropped this project in my lap. They told me they needed a database to track certain patient information, so here I am.


      Originally Posted by SteveJohnson View Post

      Depends on how your dropdowns are structured.
      Here is a copy of the drop down menu and insert codes - A lot of the other codes have been removed:

      $sql = "INSERT INTO patientformdata (birthdate)
      PrepSQL($varBirthdate) . ", " .


      <p>
      Select Birthday:

      <?php

      $display .= '</select>
      <select name="DateOfBirth_Year">
      <option value="">- Year -</option>';

      $thisyear = date('Y');
      $now = $thisyear - 2;
      $end = $thisyear - 100 ;

      for ($now; $now >= $end; $now--) {
      if ($_POST['DateOfBirth_Year'] == $now){
      $display .= '<option value="'.$now.'" selected>'.$now.'</option>';
      }
      else{
      $display .= '<option value="'.$now.'" >'.$now.'</option>';
      }
      }
      $display .= '</select> ';

      $display .='</select>
      <select name="DateOfBirth_Month">
      <option value="" >- Month -</option>';
      for ($i = 1; $i <= 12; $i++) {
      if ($_POST['DateOfBirth_Month'] == $i){
      $display .= '<option value="'.$i.'" selected>'.GetMonthString($i).'</option>';
      }
      else{
      $display .= '<option value="'.$i.'" >'.GetMonthString($i).'</option>';
      }
      }
      function GetMonthString($n){
      $timestamp = mktime(0, 0, 0, $n, 1, 2010);
      return date("F", $timestamp);
      }

      $display .= '<select name="DateOfBirth_Day">
      <option value="" >- Day -</option>';

      for ($i = 1; $i <= 31; $i++) {
      if ($_POST['DateOfBirth_Day'] == $i){
      $display .= '<option value="'.$i.'" selected>'.$i.'</option>';
      }
      else{
      $display .= '<option value="'.$i.'" >'.$i.'</option>';
      }
      }
      $display .= '</select> ';

      echo $display;

      $DateOfBirth_Year = $_POST['yyyy'];
      $DateOfBirth_Month = $_POST['mm'];
      $DateOfBirth_Day = $_POST['dd'];
      $Birthdate = "$DateOfBirth_Year, $DateOfBirth_Month, $DateOfBirth_Day"; // or whatever format you want

      ?>
      </p>
      {{ DiscussionBoard.errors[2666820].message }}
  • Profile picture of the author Tim Brownlaw
    Hi Kev,

    [Update - Kev has posted before I posted this.... just reading the above post but I'll keep what's here, here, jus tin case ]

    Without knowing anymore on this than what you've mentioned I'll give this another shot as it may give you a clue, or not!

    Steve is on the money for getting the information from the post data ( are you using method = post on your form, or get?) into the correct format.

    I'll call the birthdate - dob - for date of birth...
    Is the field in your table defined as something like...

    `dob` date NOT NULL DEFAULT '0000-00-00'

    I'll make up the name of the table to be members.
    So performing the SQL
    $sql = "INSERT INTO members (dob) values ('".$birthdate."')";

    should give you a new row with just the dob field filled in. The others, whatever they are - we don't know as you've not told us - should be the default.

    Let's step back a bit...
    What happens if you put this command in your form script.
    print_r($_REQUEST);

    This will display whats coming back in either a POST or GET.
    Iniitially it won't show much until you click on your forms Submit button.

    Maybe if you could do that and post back the results from that here.

    Cheers
    Tim
    {{ DiscussionBoard.errors[2666846].message }}
    • Profile picture of the author ~kev~
      Originally Posted by Tim Brownlaw View Post

      Without knowing anymore on this than what you've mentioned I'll give this another shot as it may give you a clue, or not!

      Sorry, I did not realize that I had not posted the code I was using. I edited my post just above yours and added the code.
      {{ DiscussionBoard.errors[2666861].message }}
  • Profile picture of the author Tim Brownlaw
    Hi Kev - yeah just caught the code now...

    First thing I can see is that the Form Select Names do not match your $_POST names...

    What I see at a glance is

    $_POST['yyyy'] should be $_POST['DateOfBirth_Year'] etc.
    Check that all of your other variable names match up too...

    The names used in the $_POST names should be the same as the form names .

    [edit - added this section]
    So
    $DateOfBirth_Year = $_POST['yyyy'];
    $DateOfBirth_Month = $_POST['mm'];
    $DateOfBirth_Day = $_POST['dd'];
    $Birthdate = "$DateOfBirth_Year, $DateOfBirth_Month, $DateOfBirth_Day";

    Should be
    $DateOfBirth_Year = $_POST['DateOfBirth_Year'];
    $DateOfBirth_Month = $_POST['$DateOfBirth_Month'];
    $DateOfBirth_Day = $_POST[' $DateOfBirth_Day'];
    $Birthdate = "$DateOfBirth_Year - $DateOfBirth_Month - $DateOfBirth_Day";


    Also watch your Case on any variables as PHP treats things like
    $thisisavariable and $ThisIsAVariable as two different beasts...
    {{ DiscussionBoard.errors[2666908].message }}
    • Profile picture of the author Tim Brownlaw
      The actual insert should be performed after the form has been tested as being submitted. I'm guessing that's done somewhere else...
      {{ DiscussionBoard.errors[2666925].message }}
  • Profile picture of the author kamleshk
    Hi
    my name is kamlesh
    i'm making a website for my college project so i have included a registration form with fields
    firstname
    lastname
    username
    password
    birthdate
    email
    sex

    i googled and got some codes i haven't tried php but trying my best

    i'm not able to get birthdate and sex in the database

    please reply immediately..........
    {{ DiscussionBoard.errors[5705385].message }}

Trending Topics