Results 1 to 6 of 6

Thread: DoB problem

  1. #1
    Join Date
    Oct 2011
    Location
    London
    Posts
    41
    Thanks
    19
    Thanked 1 Time in 1 Post

    Red face DoB problem

    why does this code output 127 no matter what year is selected

    PHP Code:
    echo '<select name="yearbirth">';
                        
    $year_from date("Y",time()) - 8;
                        
    $year_to date("Y",time()) - 100;
                        if (
    $_REQUEST['yearbirth'])
                            
    $yearkey $_REQUEST['yearbirth'];
                        else
                            
    $yearkey $year_from 12;
                        for (
    $i=$year_from;$i>$year_to;$i--) {
                            
    $selected '';
                            if (
    $yearkey == $i)
                                
    $selected 'selected';
    echo 
    '<option value="'.$i.'" '.$selected.'>'.$i.'</option>';
                        }
    echo 
    '</select>'

  2. #2
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    Quote Originally Posted by TwitterRooms View Post
    why does this code output 127 no matter what year is selected

    PHP Code:
    echo '<select name="yearbirth">';
                        
    $year_from date("Y",time()) - 8;
                        
    $year_to date("Y",time()) - 100;
                        if (
    $_REQUEST['yearbirth'])
                            
    $yearkey $_REQUEST['yearbirth'];
                        else
                            
    $yearkey $year_from 12;
                        for (
    $i=$year_from;$i>$year_to;$i--) {
                            
    $selected '';
                            if (
    $yearkey == $i)
                                
    $selected 'selected';
    echo 
    '<option value="'.$i.'" '.$selected.'>'.$i.'</option>';
                        }
    echo 
    '</select>'
    Your question is unclear.
    Please provide more information, and be as specific as possible.
    • What do you want to accomplish?
    • What have you already tried?
    • What problems did you encounter?

    Also, please be sure that you have included all relevant code and/or a link to the page in question.

    1. What do you mean, "output 127 no matter what"? It should be outputting a lot of stuff, starting with <select name="yearbirth">.

    2. What is the value of $_REQUEST['yearbirth']?

  3. The Following User Says Thank You to traq For This Useful Post:

    TwitterRooms (01-18-2012)

  4. #3
    Join Date
    Oct 2011
    Location
    London
    Posts
    41
    Thanks
    19
    Thanked 1 Time in 1 Post

    Default

    sorry its part of a form that gets DoB from user here is a bit more code
    PHP Code:
    echo '<select name="daybirth">';
                        for (
    $i=1;$i<=31;$i++) {
                            if (
    $i<10)$j='0'.$i;else $j=$i;$iss='';
                            if (
    $_REQUEST['daybirth'] == $j$iss='selected';
                            echo 
    '<option value="'.$j.'" '.$iss.'>'.$j.'</option>';
                        }
    echo 
    '</select>';
                        
    echo 
    '<select name="monthbirth">';
                        for (
    $i=1;$i<13;$i++) {
                            if (
    $i<10)$j='0'.$i;else $j=$i;$iss='';
                            if (
    $_REQUEST['monthbirth'] == $j$iss='selected';
    echo 
    '<option value="'.$j.'" '.$iss.' >'.$j.'</option>';
                        }
                        
                        
    echo 
    '</select>';

    echo 
    '<select name="yearbirth">';
                        
    $year_from date("Y",time()) - 8;
                        
    $year_to date("Y",time()) - 100;
                        if (
    $_REQUEST['yearbirth'])
                            
    $yearkey $_REQUEST['yearbirth'];
                        else
                            
    $yearkey $year_from 12;
                        for (
    $i=$year_from;$i>$year_to;$i--) {
                            
    $selected '';
                            if (
    $yearkey == $i)
                                
    $selected 'selected';
    echo 
    '<option value="'.$i.'" '.$selected.'>'.$i.'</option>';
                        }
    echo 
    '</select>';


    ?> 
    now when i enter this info into a mysql DB it puts day month all ok but year goes in as 127 and the value of $_REQUEST['yearbirth'] is unknow to me

  5. #4
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    Quote Originally Posted by TwitterRooms View Post
    when i enter this info into a mysql DB it puts day month all ok but year goes in as 127
    so, you're not asking about the form specifically, but what is inserted into the database?

    We'd need to see the code that handles the database queries.

    Quote Originally Posted by TwitterRooms View Post
    the value of $_REQUEST['yearbirth'] is unknow to me
    ...why?

    where is the value set?

  6. The Following User Says Thank You to traq For This Useful Post:

    TwitterRooms (01-18-2012)

  7. #5
    Join Date
    Oct 2011
    Location
    London
    Posts
    41
    Thanks
    19
    Thanked 1 Time in 1 Post

    Default

    mysql_query("INSERT INTO members (firstname, lastname, username, password, email, daybirth, monthbirth, yearbirth, gender, about) VALUES('$firstname','$lastname','$username','".md5($_POST[password])."','$email','$_POST[daybirth]','$_POST[monthbirth]','$_POST[yearbirth]','$_POST[gender]','$_POST[about]')");


    Like i said i'm a newbie

  8. #6
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    Quote Originally Posted by TwitterRooms View Post
    PHP Code:
    mysql_query("INSERT INTO members (firstname, lastname, username, password, email, daybirth, monthbirth, yearbirth, gender, about) VALUES('$firstname','$lastname','$username','".md5($_POST[password])."','$email','$_POST[daybirth]','$_POST[monthbirth]','$_POST[yearbirth]','$_POST[gender]','$_POST[about]')"); 
    Two things:

    1. NEVER, NEVER, NEVER put user input directly into a SQL query like that.
    ....a) Best case: hard-to-track SQL errors.
    ....b) Worst Case: Hacked database!

    Always assume that user input is "bad." Validate/ sanitize it before using it.

    2. Since you're using the $_POST value directly, we know that the $_POST['yearbirth'] value was "127".

    do you have error reporting turned on?

Bookmarks

Posting Permissions

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