如何使用他输入的 DOB 存储人的年龄但它不起作用
How to store the age of the person using his DOB entered but it's not working
这里DOB有输入文本字段的值
$DOB = $_POST['DOB'];
$today = date("d-m-Y");
$diff = date_diff(date_create($DOB), date_create($today));
echo 'Age is '.$diff->format('%y');
$a = $diff->format('%y');
$age = $_POST[$a];
这是插入查询
$sql="INSERT INTO details (`id`, `email`, `member_id`, `name`, `DOB`, `gender`, 'age')
VALUES (NULL,'$email2', '$member_id', '$name', '$DOB', '$gender', '$age')";
$query=mysqli_query($con,$sql);
我认为这是静态的问题我猜它需要使用输入文本值动态完成?如何获取输入文本值,以便存储出生日期字段中输入的年龄?
我不建议将年龄存储在数据库中。这是一个派生信息,其值随时间变化。存放后 1 年内将过时。
您可以使用大多数数据库中提供的简单日期函数直接在查询中轻松计算年龄(当然,假设您将 dob
存储为合法的 date
数据类型).假设你是 运行 MySQL:
timestampdiff(year, dob, current_date) as age
如果您要经常输入此内容,您可以考虑创建一个视图:
create view v_details as
select d.*, timestampdiff(year, dob, current_date) as age
from details d
这里DOB有输入文本字段的值
$DOB = $_POST['DOB'];
$today = date("d-m-Y");
$diff = date_diff(date_create($DOB), date_create($today));
echo 'Age is '.$diff->format('%y');
$a = $diff->format('%y');
$age = $_POST[$a];
这是插入查询
$sql="INSERT INTO details (`id`, `email`, `member_id`, `name`, `DOB`, `gender`, 'age')
VALUES (NULL,'$email2', '$member_id', '$name', '$DOB', '$gender', '$age')";
$query=mysqli_query($con,$sql);
我认为这是静态的问题我猜它需要使用输入文本值动态完成?如何获取输入文本值,以便存储出生日期字段中输入的年龄?
我不建议将年龄存储在数据库中。这是一个派生信息,其值随时间变化。存放后 1 年内将过时。
您可以使用大多数数据库中提供的简单日期函数直接在查询中轻松计算年龄(当然,假设您将 dob
存储为合法的 date
数据类型).假设你是 运行 MySQL:
timestampdiff(year, dob, current_date) as age
如果您要经常输入此内容,您可以考虑创建一个视图:
create view v_details as
select d.*, timestampdiff(year, dob, current_date) as age
from details d