Log in

View Full Version : DoB problem



TwitterRooms
01-18-2012, 06:33 PM
why does this code output 127 no matter what year is selected


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>';

traq
01-18-2012, 07:15 PM
why does this code output 127 no matter what year is selected


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']?

TwitterRooms
01-18-2012, 07:32 PM
sorry its part of a form that gets DoB from user here is a bit more 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

traq
01-18-2012, 08:40 PM
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.


the value of $_REQUEST['yearbirth'] is unknow to me...why?

where is the value set?

TwitterRooms
01-18-2012, 08:43 PM
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

traq
01-18-2012, 10:29 PM
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?