Ahora mi problema, he leído varios post donde preguntan como obtener la edad apartar de la fecha de nacimiento pero ninguno de los que he leido, logro entenderlo o ajustarlo a mis necesidades.
lo que quiero hacer es lo siguiente a ver si pueden ayudarme se los describiré detalladamente.
En una base de datos echa en mysql tengo una tabla x donde guardo los datos de una persona a través de los siguientes campos.
*codigo int
nombre varchar
apellido varchar
fecha_nacimiento date
edad int
lo que me gustaría hacer es que al ingresar a través de un formulario estos datos hacia la respectiva tabla (hasta aquí no hay problema) al colocar la fecha de nacimiento(en su respectivo campo) me colocara la edad automáticamente(en su respectivo campo comparándola con la fecha actual y siempre que haga una consulta esta(la edad) se mantenga actualizada....
esta es la pagina que contiene el formulario donde se envian los datos:
Código PHP:
<?php require_once('../Connections/modulo_1.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}
$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
$insertSQL = sprintf("INSERT INTO conductor (cedula, nombre, apellido, fecha_nacimiento, edad) VALUES (%s, %s, %s, %s, %s)",
GetSQLValueString($_POST['cedula'], "int"),
GetSQLValueString($_POST['nombre'], "text"),
GetSQLValueString($_POST['apellido'], "text"),
GetSQLValueString($_POST['fecha_nacimiento'], "date"),
GetSQLValueString($_POST['edad'], "int"));
mysql_select_db($database_modulo_1, $modulo_1);
$Result1 = mysql_query($insertSQL, $modulo_1) or die(mysql_error());
$insertGoTo = "../usuario_registrado.php";
if (isset($_SERVER['QUERY_STRING'])) {
$insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
$insertGoTo .= $_SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $insertGoTo));
}
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>obtener fecha de nacimiento</title>
</head>
<body>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<th scope="col"> </th>
</tr>
<tr>
<th scope="row"> </th>
</tr>
<tr>
<th scope="row">
<form action="<?php echo $editFormAction; ?>" method="post" name="form1" id="form1">
<table align="center">
<tr valign="baseline">
<td nowrap="nowrap" align="right">Cedula:</td>
<td><input type="text" name="cedula" value="" size="32" /></td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right">Nombre:</td>
<td><input type="text" name="nombre" value="" size="32" /></td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right">Apellido:</td>
<td><input type="text" name="apellido" value="" size="32" /></td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right">Fecha de nacimiento:</td>
<td><input type="text" name="fecha_nacimiento" value="" size="32" /></td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right">Edad:</td>
<td><input type="text" name="edad" value="" size="32" /></td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right"> </td>
<td><input type="submit" value="Insertar registro" /></td>
</tr>
</table>
<input type="hidden" name="MM_insert" value="form1" />
</form>
<p> </p></th>
</tr>
<tr>
<th scope="row"> </th>
</tr>
</table>
</body>
</html>
Saludos...