Ver Mensaje Individual
  #5 (permalink)  
Antiguo 04/09/2012, 16:42
yatant
 
Fecha de Ingreso: marzo-2012
Ubicación: mexicali
Mensajes: 2
Antigüedad: 12 años, 1 mes
Puntos: 0
Respuesta: Problema de codigo

Si se puede solo tienes que poner el codigo del insert arriba antes de utilizar un echo.

prueba esto:
Código PHP:
Ver original
  1. <?php
  2.  
  3. function empty_array($vars){
  4.     foreach($vars as $value)
  5.     {
  6.         if(empty($_POST[$value]))
  7.         {
  8.             return true;
  9.         }
  10.     }
  11.     return false;
  12. }
  13.  
  14. $vars = array('nombre','apellidos' ,'pais','ciudad','fecha','usuario' , 'contrasena','email','descripcion');
  15. //Especifico que si está relleno el campo y no está vacio, realiza las instrucciones de conexión con la BD.
  16. if(!empty_array($vars))
  17. {
  18.     foreach($vars as $value)
  19.     {
  20.         $$value = htmlspecialchars($_POST[$value]);
  21.     }
  22.    
  23.     //Incluyo el archivo donde tengo los datos de conexión.
  24.     include_once("conexion.php");
  25.     $con = new mysqli($host,$user,$pw,$db);
  26.     {
  27.         die('Error al conectar con la base de datos');
  28.     }
  29.    
  30.  
  31.  
  32.     //Realizo la consulta
  33.     $q = "INSERT INTO usuarios (Nombre, Apellidos, Pais, Ciudad, Fechanacimiento, Usuario, Pass, Email,                 Descripcion)";
  34.     $q .= " VALUES ('$nombre', '$apellidos', '$pais', '$ciudad', '$fechanacimiento', '$usuario', '$pass', '$email',     '$descripcion')" ;
  35.    
  36.     //Lanzo una excepción por si hay errores
  37.     try{
  38.         if(!$con -> query($q))
  39.             throw new Exception (" Ha habido un error en su registro, intentelo de nuevo mas tarde ");
  40.         else
  41.             header('Location: pagina_a_redireccionar.php');
  42.     }      
  43.     catch (Exception $e)
  44.     {
  45.         echo $e -> getMessage();
  46.     }
  47. }
  48.    
  49. if (empty($_POST['button']))
  50. {
  51.     $primeravez = TRUE;
  52.     foreach($vars as $value)
  53.     {
  54.         $_POST[$value] = "";
  55.     }
  56. }
  57. ?>
  58. <html>
  59.  
  60.     <head>
  61.     <link href="estilo.css" rel="stylesheet" type="text/css">
  62.     <link href="estilos.css" rel="stylesheet" type="text/css">
  63.     </head>
  64.  
  65.     <body>
  66.         <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
  67.           <table width="926" border="2" align="center">
  68.             <tr>
  69.               <td height="60" colspan="2" align="center"><h2>FORMULARIO DE REGISTRO</h2></td>
  70.             </tr>
  71.                 <?php
  72.                     foreach($vars as $value){
  73.                         echo'<tr>
  74.                         <td width="466" height="39">'.strtoupper($value).':</td>
  75.                         <td width="596"><input type="text" name="'.$value.'" id="'.$value.'" value="'.$_POST[$value].'"></td>';
  76.  
  77.                         if(empty ($_POST[$var]) && (empty($primeravez)))
  78.                         {
  79.                             echo " <tr><td></td><td class = 'error'> Tiene que introducir $value  </td></tr>";
  80.                         }  
  81.                     }
  82.                 ?>
  83.             <tr>
  84.               <td height="66" colspan="2" align="center"><input type="submit" name="button" id="button" value="REGISTRARSE"></td>
  85.             </tr>
  86.         </table>
  87.         </form>
  88.     </body>
  89. </html>