Código:
<?php
$primeravez = FALSE;
if (empty($_POST['button'])){
$primeravez = TRUE;
$_POST['nombre'] = "";
$_POST['apellidos'] = "";
$_POST['pais'] = "";
$_POST['ciudad'] = "";
$_POST['fecha'] = "";
$_POST['usuario'] = "";
$_POST['contrasena'] = "";
$_POST['email'] = "";
$_POST['descripcion'] = "";
}
?>
<html>
<head>
<link href="estilo.css" rel="stylesheet" type="text/css">
<link href="estilos.css" rel="stylesheet" type="text/css">
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<table width="926" border="2" align="center">
<tr>
<td height="60" colspan="2" align="center"><h2>FORMULARIO DE REGISTRO</h2></td>
</tr>
<tr>
<td width="466" height="39">Nombre:</td>
<td width="596"><input type="text" name="nombre" id="nombre" value="<?php echo $_POST['nombre']; ?>"></td>
</tr>
<?php
if(empty ($_POST['nombre']) && (!$primeravez)) {
echo " <tr><td></td><td class = 'error'> Tiene que introducir su Nombre </td></tr>";
}
?>
<tr>
<td height="41">Apellidos:</td>
<td><input type="text" name="apellidos" id="apellidos" value="<?php echo $_POST['apellidos']; ?>"></td>
</tr>
<?php
if(empty ($_POST['apellidos']) && (!$primeravez)) {
echo " <tr><td></td><td class = 'error'> Tiene que introducir sus Apellidos </td></tr>";
}
?>
<tr>
<td height="38">Pais:</td>
<td><input type="text" name="pais" id="pais" value="<?php echo $_POST['pais']; ?>"></td>
</tr>
<?php
if(empty ($_POST['pais']) && (!$primeravez)) {
echo " <tr><td></td><td class = 'error'> Tiene que introducir su Pais </td></tr>";
}
?>
<tr>
<td height="35">Ciudad:</td>
<td><input type="text" name="ciudad" id="ciudad" value="<?php echo $_POST['ciudad']; ?>"></td>
</tr>
<?php
if(empty ($_POST['ciudad']) && (!$primeravez)) {
echo " <tr><td></td><td class = 'error'> Tiene que introducir su Ciudad </td></tr>";
}
?>
<tr>
<td height="32">Fecha de Nacimiento:</td>
<td><input name="fecha" type="text" id="fecha" value="<?php echo $_POST['fecha']; ?>"></td>
</tr>
<?php
if(empty ($_POST['fecha']) && (!$primeravez)) {
echo " <tr><td></td><td class = 'error'> Tiene que introducir su Fecha de Nacimiento </td></tr>";
}
?>
<tr>
<td height="36">Usuario:</td>
<td><input type="text" name="usuario" id="usuario" value="<?php echo $_POST['usuario']; ?>"></td>
</tr>
<?php
if(empty ($_POST['usuario']) && (!$primeravez)) {
echo " <tr><td></td><td class = 'error'> Tiene que introducir su Usuario </td></tr>";
}
?>
<tr>
<td height="36">Password:</td>
<td><input type="password" name="contrasena" id="contrasena" value="<?php echo $_POST['contrasena']; ?>"></td>
</tr>
<?php
if(empty ($_POST['contrasena']) && (!$primeravez)) {
echo " <tr><td></td><td class = 'error'> Tiene que introducir su Password </td></tr>";
}
?>
<tr>
<td height="34">Email:</td>
<td><input name="email" type="text" id="email" value="<?php echo $_POST['email']; ?>"></td>
</tr>
<?php
if(!$primeravez)
if(empty ($_POST['email']))
echo " <tr><td></td><td class = 'error'> Tiene que introducir su Email </td></tr>";
elseif(!preg_match ("/^(([A-Za-z0-9]+_+)|([A-Za-z0-9]+\-+)|([A-Za-z0-9]+\.+)|([A-Za-z0-9]+\++))*[A-Za-z0-9]+@((\w+\-+)|(\w+\.))*\w{1,63}\.[a-zA-Z]{2,6}$/", $_POST['email']))
echo "<tr><td></td><td class = 'error'> Formato de mail no valido </td></tr>";
?>
<tr>
<td height="33">Algo sobre ti:</td>
<td><textarea name="descripcion" id="descripcion" value="<?php echo $_POST['descripcion']; ?>" cols="50" rows="5"></textarea></td>
</tr>
<?php
if(empty ($_POST['descripcion']) && (!$primeravez)) {
echo " <tr><td></td><td class = 'error'> Tiene que introducir algo sobre usted </td></tr>";
}
?>
<tr>
<td height="66" colspan="2" align="center"><input type="submit" name="button" id="button" value="REGISTRARSE"></td>
</tr>
</table>
</form>
<?php
//Especifico que si está relleno el campo y no está vacio, realiza las instrucciones de conexión con la BD.
if(isset ($nombre) && !empty ($nombre) && ($apellidos) && !empty ($apellidos) && ($pais) && !empty ($pais) && ($ciudad) && !empty ($ciudad) && ($fechanacimiento) && !empty ($fechanacimiento) && ($usuario) && !empty ($usuario) && ($pass) && !empty ($pass) && ($email) && !empty ($email) && ($descripcion) && !empty ($descripcion)){
//Incluyo el archivo donde tengo los datos de conexión.
include_once("conexion.php");
$con = new mysqli($host,$user,$pw,$db);
if(mysqli_connect_errno()){
echo " Error al conectar con la base de datos ";
exit();
}
//Guardo las variables recibidas desde el formulario
$nombre = htmlspecialchars($_POST['nombre']);
$apellidos = htmlspecialchars($_POST['apellidos']);
$pais = htmlspecialchars($_POST['pais']);
$ciudad = htmlspecialchars($_POST['ciudad']);
$fechanacimiento = htmlspecialchars($_POST['fecha']);
$usuario = htmlspecialchars($_POST['usuario']);
$pass = htmlspecialchars($_POST['contrasena']);
$email = htmlspecialchars($_POST['email']);
$descripcion = htmlspecialchars($_POST['descripcion']);
//Realizo la consulta
$q = "INSERT INTO usuarios (Nombre, Apellidos, Pais, Ciudad, Fechanacimiento, Usuario, Pass, Email, Descripcion)";
$q .= " VALUES ('$nombre', '$apellidos', '$pais', '$ciudad', '$fechanacimiento', '$usuario', '$pass', '$email', '$descripcion')" ;
//Lanzo una excepción por si hay errores
try{
if(!$con -> query($q))
throw new Exception (" Ha habido un error en su registro, intentelo de nuevo mas tarde ");
else
echo " Gracias por registrarse ";
}
catch (Exception $e){
echo $e -> getMessage();
}
}
?>
</body>
</html>


