Jump to content

[SOLVED] Stumped.


ebats

Recommended Posts

I've got a form submitting into a mysql database through php.  the php verifies the age over 13 and then enters the fields.  It's been working for one day and now has suddenly just been sending all entries to the under 13 error page.  My only thought is somehow i'm getting the error through duplicate entries in the database, because the age calculation looks right.  Any help would be much appreciated.

 

<?php
$month = $_REQUEST['month'];
$day = $_REQUEST['day'];
$year = $_REQUEST['year'];
$this_day = date(d);
$this_month = date(m);
$this_year = date(Y);
$day_val = $this_day - $day;
$month_val = $this_month - $month;
$year_val = $this_year - $year;
if($year_val >= 13&&$month_val >= 0&&$day_val >= 0) {
$con = mysql_connect("SERVER","DATABASE","PASSWORD");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }mysql_select_db("DATABASE", $con);$sql="INSERT INTO TABLENAME(FirstName, LastName, email, month, day, year)
VALUES
('$_POST[FirstName]','$_POST[LastName]','$_POST[email]','$_POST[month]','$_POST[day]','$_POST[year]')";if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
mysql_close($con);
header('Location: thankyou.html');
} 
else {     
header('Location: sorry.html');
}
?>

 

Link to comment
https://forums.phpfreaks.com/topic/131834-solved-stumped/
Share on other sites

First, PLEASE put your code in


tags. Second, it is best to practice handling arrays in strings like this:

" some text {$_POST['value']} blah blah";

rather than this way:

" some text $_POST[value] blah blah";

 

Finally, what do you mean that it is sending them to the error page? What is the error page?

Link to comment
https://forums.phpfreaks.com/topic/131834-solved-stumped/#findComment-684851
Share on other sites

Firstly, you logic is wrong. With the current check, anyone born on a day or month greater than today's day or month will not pass. Secondly, it appears you have errors/warnings turned off - you're missing some quotes around the parameters in the date() functions:

 

$this_day = date(d);
$this_month = date(m);
$this_year = date(Y);

Link to comment
https://forums.phpfreaks.com/topic/131834-solved-stumped/#findComment-684867
Share on other sites

Easier way of doing the calculation:

 

<?php
$dob = strtotime("{$_REQUEST['year']}-{$_REQUEST['month']}-{$_REQUEST['day']}");
$diff = time() - $dob;
$years = gmdate('Y', $diff) - 1970;
if ($years >= 13) {
//DOB was at least 13 years ago
} else {
//no go
}
?>

Link to comment
https://forums.phpfreaks.com/topic/131834-solved-stumped/#findComment-684874
Share on other sites

No problem, glad it works.

 

I was about to post this:

 

Oh, maybe you need to pad the user input with leading zeros:

 

<?php
$m = str_pad($_REQUEST['month'], 2, '0', STR_PAD_LEFT);
$d = str_pad($_REQUEST['day'], 2, '0', STR_PAD_LEFT);
$dob = strtotime("{$_REQUEST['year']}-$m-$d");
$diff = time() - $dob;
$years = gmdate('Y', $diff) - 1970;
if ($years >= 13) {
//DOB was at least 13 years ago
} else {
//no go
}
?>

 

Maybe it's required for single digit months or days.

Link to comment
https://forums.phpfreaks.com/topic/131834-solved-stumped/#findComment-684907
Share on other sites

Thought you may ask ;) Chances are you already know that a Unix timestamp, returned by strtotime() and time(), is the number of seconds since 1970-01-01 00:00:00 GMT (called the Unix Epoch). That makes $diff relative to the Unix Epoch, when we use it as a timestamp in the GMT date function gmdate(). To calculate the time span, we need to take the Unix Epoch into account. E.g. if $diff is 1 second, gmdate('Y-m-d H:i:s', $diff) would return "1970-01-01 00:00:01". Subtracting 1970 from the years and 1 from the months and days, gives us the time span we're looking for, i.e. "0000-00-00 00:00:01". But why the hassle, you may ask? Using gmdate() is more precise (and faster) than converting the difference in seconds to years, months, days, etc. using simple maths, since it's hard to count on the averages for seconds per year and seconds per month (an average year is 365.2425 days and an average month is a 30.436875 days).

Link to comment
https://forums.phpfreaks.com/topic/131834-solved-stumped/#findComment-685223
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.