im attempting to add date of birth and age to a form and database the date of birth works fine and shows in database as it should be but the age is showing 0 in age column ive tried different things after searching and its staying the same

heres the code for age and date of birth in the php block

$year = $_POST['year'];
$month = $_POST['month'];
$day = $_POST['day'];
$DOB = $year . "-" . $month . "-" . $day;
$age = $_POST['age'];

And here is the database string

mysqli_query($conn,"INSERT INTO signup (f_name,email,password,DOB,age,memtype) VALUES ('$fname','$email','$EncryptPassword','$DOB','$age','$memtype')"); 

date of birth is set as DATE database
age is set as int 5 in database

is there anythin gim missing of not quite right ty in advance jan x

Hi,

it seems fine. Are you sure $_POST['age'] is set? Check the submit form or do a var_dump($_POST); to see what is really sent with the request, maybe is not age but Age or something similar.

ive checked all the age eliments and there's no typos etc here s the complete php code for signup.php

<?php
include 'dbconfig.php';

if(isset($_POST["submit"]))
{

$fname = $_POST["Fname"];
$email = $_POST["email"];
$password = $_POST["password"];
$year = $_POST['year'];
$month = $_POST['month'];
$day = $_POST['day'];
$DOB = $year . "-" . $month . "-" . $day;
$age = $_POST['age'];
$country = $_POST['country'];

$memtype = $_POST["memtype"];

$EncryptPassword = md5($password);

mysqli_query($conn,"INSERT INTO signup (f_name,email,password,DOB,age,memtype,country) VALUES ('$fname','$email','$EncryptPassword','$DOB','$age','$memtype','$country')"); 

echo '<center> Sign Up form successfully submitted. </center>';

}

 ?>

Just done var dump and here s the results from it

array(9) { ["Fname"]=> string(5) "kevin" ["password"]=> string(8) "password" ["email"]=> string(19) "testing@testing.com" ["day"]=> string(2) "17" ["month"]=> string(1) "1" ["year"]=> string(4) "1961" ["memtype"]=> string(9) "Couple MF" ["country"]=> string(7) "England" ["submit"]=> string(12) "Submit Query" }
Sign Up form successfully submitted. 
Member Avatar for diafol

So where is $_POST['age'] ? You can see yourself that you have no ["age"] item in the dump.

Not sure you should store current age anyway, otherwise you'll need to set a cronjob to update ages every day at midnight your local time. Ages can be calculated on the fly - either via SQL or php.

And BTW - you are wide open to SQL injection - there is no sanitizing nor use prepared statements.

/// edit
Sorry D, just saw your post! ;D

As you see, from the dump, age is not set in the POST request and there is nothing similar, a part the DOB from which you could calculate the age:

$birth = "{$year}-{$month}-{$day}";
$dob = new DateTime($birth);
$now = new DateTime();
$age = $now->diff($dob)->format('%y');

If you have an input field named age in the form, then make sure it is inside the correct <form> scope.

A part that, md5 is very weak choice to save passwords, use password_hash() and password_verify() to perform the check. See:

yes only just started on this one but ill look at the php calculations thankyou very much jan x

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.