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.
Page 1 of 2 12 LastLast
Results 1 to 15 of 19
  1. #1
    New Coder
    Join Date
    Dec 2011
    Posts
    32
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Question Putting $date, $month and $year variable into one $dob field in a MySQL Database..

    I have just had some wonderful help off a very helpful member regarding php and dob etc and got my calendar working and all this but the problem I have is that I can't work out how to make the $day $month and $year variables post into a date field "dob" in my MySQL Database... I am not sure if this is even possible

    PHP Code:
    $dob=$_POST['dob'];  

    $daysubstr($dob02);  
    $month substr($dob32);  
    $year substr($dob64); 
    I also have this:

    PHP Code:
    Add user info into the database table for the main site table
         $sql 
    mysql_query("INSERT INTO myMembers (firstname, lastname, country, state, city, dob, email, password, sign_up_date, website, youtube) 
         VALUES('$firstname','$lastname','$country','$state','$city', '$dob', '$email1','$db_password', now(),'$website','$youtube')"
    )  
         or die (
    mysql_error());
     
         
    $id mysql_insert_id(); 
    To summarise I want the date entered on my site to be put into my field "dob" in my MySQL database when submit is clicked

    PS Dont fill it in and click submit because the activation part isn't complete meaning nothing will work for you
    Last edited by Fou-Lu; 01-04-2012 at 01:38 AM.

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,200
    Thanks
    75
    Thanked 4,342 Times in 4,308 Posts
    Code:
    $dob=$_POST['dob'];  
    
    $day= substr($dob, 0, 2);  
    $month = substr($dob, 3, 2);  
    $year = substr($dob, 6, 4); 
    
    // use THIS dob for the SQL insert:
    $sqldob = $year . "-" . $month . "-" . $day;
    MySQL wants to always see dates in the form 'yyyy-mm-dd' (it's okay to only give one digit for month or day, also, as in '2012-1-3' for January 3rd).
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    New Coder
    Join Date
    Dec 2011
    Posts
    32
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Date Format isn't an issue luckily

    So I remove the mysql_query("INSERT INTO myMembers (dob) and value counterpart
    and just put this bit of code
    the
    Code:
    $sqldob = $year . "-" . $month . "-" . $day;
    or wait.. I add that bit of code you have given me and leave the rest don't I?

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,200
    Thanks
    75
    Thanked 4,342 Times in 4,308 Posts
    Add that one line, as shown, and then change your INSERT just a tiny bit:
    Code:
         $sql = mysql_query("INSERT INTO myMembers (firstname, lastname, country, state, city, dob, email, password, sign_up_date, website, youtube) 
         VALUES('$firstname','$lastname','$country','$state','$city', '$sqldob', '$email1','$db_password', now(),'$website','$youtube')")  
         or die (mysql_error());
    Just change $dob to $sqldob in the INSERT.

    There is nothing magic about the name $sqldob. You could have use $zyzzyg or $fizzle or $FRAP both places, instead, if you wished. I just named it that to emphasize that it was indeed a DOB value intended for use with SQL.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #5
    New Coder
    Join Date
    Dec 2011
    Posts
    32
    Thanks
    1
    Thanked 2 Times in 2 Posts
    This hasn't worked? I am still left with dob=0000-00-00 after I register..
    Is it that dob needs to be called sqldob in my database or not?
    Thanks for your help so far

  • #6
    New Coder
    Join Date
    Nov 2011
    Location
    New England
    Posts
    62
    Thanks
    0
    Thanked 10 Times in 10 Posts
    Quote Originally Posted by StevenPrice View Post
    This hasn't worked? I am still left with dob=0000-00-00 after I register..
    Is it that dob needs to be called sqldob in my database or not?
    Thanks for your help so far
    StephenPrice, if you are going to end up with the string 'YYYY-MM-DD' in your PHP, I would go back to the calendar and change the layout.

    This is what the example is on the website:

    Code:
    onclick="javascript:NewCssCal ('dob','yyyyMMdd')"
    That way it will already be in yyyy-mm-dd format. Then you don't need substr to parse the format from mm-dd-yyyy to yyyy-mm-dd, and can do this:

    PHP Code:
    $sqldb $_POST['dob']; 

    Also, it looks like they have a better 'arrow' way of changing months/years. All you have to do is add 'arrow' to the javascript function:

    Code:
    onclick="javascript:NewCssCal ('dob','mmddyyyy','arrow')"
    Edit:
    Also, Old_Pendant is just saying you have to have the variable named the same thing both places. [When you assign it, and when you place it into the database].
    For programming information, visit irnsystems.com
    Also check out Points2Survey and earn items.

  • #7
    New Coder
    Join Date
    Dec 2011
    Posts
    32
    Thanks
    1
    Thanked 2 Times in 2 Posts
    This has still not worked Is there any way of me putting a date of birth field into my registration form and then sending it to my database?

  • #8
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,200
    Thanks
    75
    Thanked 4,342 Times in 4,308 Posts
    Time to learn how to DEBUG DEBUG DEBUG.

    Code:
    $dob=$_POST['dob'];  
    echo "<hr/>DEBUG dob starts as " . $dob . "<br/>\n";
    
    $day= substr($dob, 0, 2);  
    $month = substr($dob, 3, 2);  
    $year = substr($dob, 6, 4); 
    
    // use THIS dob for the SQL insert:
    $sqldob = $year . "-" . $month . "-" . $day;
    echo "DEBUG sqldob is then " . $sqldob . "<br/>\n";
    
    $sqltext="INSERT INTO myMembers (firstname, lastname, country, state, city, dob, email, password, sign_up_date, website, youtube)"
            . " VALUES('$firstname','$lastname','$country','$state','$city', '$sqldob', '$email1','$db_password', now(),'$website','$youtube')";
    echo "DEBUG sqltext is " . $sqltext . "<hr/>\n";
    
    $sql = mysql_query( $sqltext ) or die (mysql_error());
    Show us what those 3 DEBUG lines show you.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    StevenPrice (12-21-2011)

  • #9
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,200
    Thanks
    75
    Thanked 4,342 Times in 4,308 Posts
    Is there any way of me putting a date of birth field into my registration form and then sending it to my database?
    What do you think we are trying to do????

    That's exactly what you are attempting.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #10
    New Coder
    Join Date
    Dec 2011
    Posts
    32
    Thanks
    1
    Thanked 2 Times in 2 Posts
    I meant other sorry

  • #11
    New Coder
    Join Date
    Dec 2011
    Posts
    32
    Thanks
    1
    Thanked 2 Times in 2 Posts
    and can I do this tomorrow as I have to go now. Will you be on tomorrow at any point Old Pedan?
    I thank you for all your help so far but I will continue tomorrow

  • #12
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,200
    Thanks
    75
    Thanked 4,342 Times in 4,308 Posts
    I'm usually here around this time of day. See you then.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #13
    New Coder
    Join Date
    Dec 2011
    Posts
    32
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Hi Old Pedant, it says your online so let's start again and try and make this thing work!
    Time to debug! So I just go into my register.php code and do what you say yeah?

    Just to clarify what should my MySQL table field say.. Just dob or sql dob or what?

  • #14
    New Coder
    Join Date
    Dec 2011
    Posts
    32
    Thanks
    1
    Thanked 2 Times in 2 Posts
    It is just dob isn't it and how do I show you the values? FTP it up onto the server?

  • #15
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,200
    Thanks
    75
    Thanked 4,342 Times in 4,308 Posts
    Just copy/paste the output you see in the browser to here.

    For those 3 DEBUG lines.

    And yes, the field name in the DB does not matter, so long as you use the same name in the query. Note that the PHP variable name ($sqldob in my example) DOES NOT MATTER. You could use $flzie11 and MySQL wouldn't care.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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