Foros del Web » Programando para Internet » PHP »

Al raro con header oO

Estas en el tema de Al raro con header oO en el foro de PHP en Foros del Web. Buenas! Tengo una pagina para insertar registros a una db mysql. envio datos de la página solicitar_usuario.php que tiene el formulario a la pagina insertar_solicitud.php ...
  #1 (permalink)  
Antiguo 05/08/2012, 11:34
 
Fecha de Ingreso: julio-2012
Ubicación: Asunción
Mensajes: 54
Antigüedad: 11 años, 9 meses
Puntos: 3
Al raro con header oO

Buenas!
Tengo una pagina para insertar registros a una db mysql.
envio datos de la página solicitar_usuario.php que tiene el formulario
a la pagina insertar_solicitud.php con variables $_POST
luego en las primeras líneas valido si los campos son vacíos y direcciono en el caso de que asi sea con un header.
despues de las lineas de validacion pongo la consulta SQL y la inserto
el problema es.

que si ocurre un error (campo vacío), me direcciona tal como quiero, pero a su vez me inserta en la base de datos o.O
aquí esta el código:

Código PHP:
Ver original
  1. if(isset($_POST['cargo']) && $_POST['cargo'] == "")
  2. {
  3.     if(isset($_POST['codigo_humano']) && $_POST['codigo_humano'] != "")
  4.     {
  5.         $codigo_humano_sent = $_POST['codigo_humano'];
  6.     }elseif(!isset($_POST['codigo_humano'])){
  7.         $codigo_humano_sent = "";
  8.     }
  9.     if(isset($_POST['fecha_nac']) && $_POST['fecha_nac'] != "")
  10.     {
  11.         $fecha_nac_sent = $_POST['fecha_nac'];
  12.     }elseif(!isset($_POST['fecha_nac'])){
  13.         $fecha_nac_sent = "";
  14.     }
  15.     if(isset($_POST['telefono']) && $_POST['telefono'] != "")
  16.     {
  17.         $telefono_sent = $_POST['telefono'];
  18.     }elseif(!isset($_POST['telefono'])){
  19.         $telefono_sent = "";
  20.     }
  21.     if(isset($_POST['email']) && $_POST['email'] != "")
  22.     {
  23.         $email_sent = $_POST['email'];
  24.     }elseif(!isset($_POST['email'])){
  25.         $email_sent = "";
  26.     }
  27.     if(isset($_POST['ubicacion']) && $_POST['ubicacion'] != "")
  28.     {
  29.         $ubicacion_sent = $_POST['ubicacion'];
  30.     }elseif(!isset($_POST['ubicacion'])){
  31.         $ubicacion_sent = "";
  32.     }
  33. #  
  34.     if(isset($_POST['telefono2']) && $_POST['telefono2'] != "")
  35.     {
  36.         $telefono2_sent = $_POST['telefono2'];
  37.     }elseif(!isset($_POST['telefono2'])){
  38.         $telefono2_sent = "";
  39.     }
  40.     if(isset($_POST['direccion']) && $_POST['direccion'] != "")
  41.     {
  42.         $direccion_sent = $_POST['direccion'];
  43.     }elseif(!isset($_POST['direccion'])){
  44.         $direccion_sent = "";
  45.     }
  46.     if(isset($_POST['hijos']) && $_POST['hijos'] != "")
  47.     {
  48.         $hijos_sent = $_POST['hijos'];
  49.     }elseif(!isset($_POST['hijos'])){
  50.         $hijos_sent = "";
  51.     }
  52.     if(isset($_POST['nombre_hijo1']) && $_POST['nombre_hijo1'] != "")
  53.     {
  54.         $nombre_hijo1_sent = $_POST['nombre_hijo1'];
  55.     }elseif(!isset($_POST['nombre_hijo1'])){
  56.         $nombre_hijo1_sent = "";
  57.     }
  58.     if(isset($_POST['fecha_nombre_hijo1']) && $_POST['fecha_nombre_hijo1'] != "")
  59.     {
  60.         $fecha_nombre_hijo1_sent = $_POST['fecha_nombre_hijo1'];
  61.     }elseif(!isset($_POST['fecha_nombre_hijo1'])){
  62.         $fecha_nombre_hijo1_sent = "";
  63.     }
  64.     if(isset($_POST['nombre_hijo2']) && $_POST['nombre_hijo2'] != "")
  65.     {
  66.         $nombre_hijo2_sent = $_POST['nombre_hijo2'];
  67.     }elseif(!isset($_POST['nombre_hijo2'])){
  68.         $nombre_hijo2_sent = "";
  69.     }
  70.     if(isset($_POST['fecha_nombre_hijo2']) && $_POST['fecha_nombre_hijo2'] != "")
  71.     {
  72.         $fecha_nombre_hijo2_sent = $_POST['fecha_nombre_hijo2'];
  73.     }elseif(!isset($_POST['fecha_nombre_hijo2'])){
  74.         $fecha_nombre_hijo2_sent = "";
  75.     }
  76.     if(isset($_POST['nombre_hijo3']) && $_POST['nombre_hijo3'] != "")
  77.     {
  78.         $nombre_hijo3_sent = $_POST['nombre_hijo3'];
  79.     }elseif(!isset($_POST['nombre_hijo3'])){
  80.         $nombre_hijo3_sent = "";
  81.     }
  82.     if(isset($_POST['fecha_nombre_hijo3']) && $_POST['fecha_nombre_hijo3'] != "")
  83.     {
  84.         $fecha_nombre_hijo3_sent = $_POST['fecha_nombre_hijo3'];
  85.     }elseif(!isset($_POST['fecha_nombre_hijo3'])){
  86.         $fecha_nombre_hijo3_sent = "";
  87.     }
  88.     if(isset($_POST['nombre_hijo4']) && $_POST['nombre_hijo4'] != "")
  89.     {
  90.         $nombre_hijo4_sent = $_POST['nombre_hijo4'];
  91.     }elseif(!isset($_POST['nombre_hijo4'])){
  92.         $nombre_hijo4_sent = "";
  93.     }
  94.     if(isset($_POST['fecha_nombre_hijo4']) && $_POST['fecha_nombre_hijo4'] != "")
  95.     {
  96.         $fecha_nombre_hijo4_sent = $_POST['fecha_nombre_hijo4'];
  97.     }elseif(!isset($_POST['fecha_nombre_hijo4'])){
  98.         $fecha_nombre_hijo4_sent = "";
  99.     }
  100.     if(isset($_POST['nombre_hijo5']) && $_POST['nombre_hijo5'] != "")
  101.     {
  102.         $nombre_hijo5_sent = $_POST['nombre_hijo5'];
  103.     }elseif(!isset($_POST['nombre_hijo5'])){
  104.         $nombre_hijo5_sent = "";
  105.     }
  106.     if(isset($_POST['fecha_nombre_hijo5']) && $_POST['fecha_nombre_hijo5'] != "")
  107.     {
  108.         $fecha_nombre_hijo5_sent = $_POST['fecha_nombre_hijo5'];
  109.     }elseif(!isset($_POST['fecha_nombre_hijo5'])){
  110.         $fecha_nombre_hijo5_sent = "";
  111.     }
  112.    
  113.     header("Location: solicitar_usuario.php?con todas las variables definidas");
  114. }
  115.  
  116. if(isset($_POST['ubicacion']) && $_POST['ubicacion'] == "")
  117. {
  118.         if(isset($_POST['codigo_humano']) && $_POST['codigo_humano'] != "")
  119.     {
  120.         $codigo_humano_sent = $_POST['codigo_humano'];
  121.     }elseif(!isset($_POST['codigo_humano'])){
  122.         $codigo_humano_sent = "";
  123.     }
  124.     if(isset($_POST['fecha_nac']) && $_POST['fecha_nac'] != "")
  125.     {
  126.         $fecha_nac_sent = $_POST['fecha_nac'];
  127.     }elseif(!isset($_POST['fecha_nac'])){
  128.         $fecha_nac_sent = "";
  129.     }
  130.     if(isset($_POST['telefono']) && $_POST['telefono'] != "")
  131.     {
  132.         $telefono_sent = $_POST['telefono'];
  133.     }elseif(!isset($_POST['telefono'])){
  134.         $telefono_sent = "";
  135.     }
  136.     if(isset($_POST['email']) && $_POST['email'] != "")
  137.     {
  138.         $email_sent = $_POST['email'];
  139.     }elseif(!isset($_POST['email'])){
  140.         $email_sent = "";
  141.     }
  142.     if(isset($_POST['ubicacion']) && $_POST['ubicacion'] != "")
  143.     {
  144.         $ubicacion_sent = $_POST['ubicacion'];
  145.     }elseif(!isset($_POST['ubicacion'])){
  146.         $ubicacion_sent = "";
  147.     }
  148. #  
  149.     if(isset($_POST['telefono2']) && $_POST['telefono2'] != "")
  150.     {
  151.         $telefono2_sent = $_POST['telefono2'];
  152.     }elseif(!isset($_POST['telefono2'])){
  153.         $telefono2_sent = "";
  154.     }
  155.     if(isset($_POST['direccion']) && $_POST['direccion'] != "")
  156.     {
  157.         $direccion_sent = $_POST['direccion'];
  158.     }elseif(!isset($_POST['direccion'])){
  159.         $direccion_sent = "";
  160.     }
  161.     if(isset($_POST['hijos']) && $_POST['hijos'] != "")
  162.     {
  163.         $hijos_sent = $_POST['hijos'];
  164.     }elseif(!isset($_POST['hijos'])){
  165.         $hijos_sent = "";
  166.     }
  167.     if(isset($_POST['nombre_hijo1']) && $_POST['nombre_hijo1'] != "")
  168.     {
  169.         $nombre_hijo1_sent = $_POST['nombre_hijo1'];
  170.     }elseif(!isset($_POST['nombre_hijo1'])){
  171.         $nombre_hijo1_sent = "";
  172.     }
  173.     if(isset($_POST['fecha_nombre_hijo1']) && $_POST['fecha_nombre_hijo1'] != "")
  174.     {
  175.         $fecha_nombre_hijo1_sent = $_POST['fecha_nombre_hijo1'];
  176.     }elseif(!isset($_POST['fecha_nombre_hijo1'])){
  177.         $fecha_nombre_hijo1_sent = "";
  178.     }
  179.     if(isset($_POST['nombre_hijo2']) && $_POST['nombre_hijo2'] != "")
  180.     {
  181.         $nombre_hijo2_sent = $_POST['nombre_hijo2'];
  182.     }elseif(!isset($_POST['nombre_hijo2'])){
  183.         $nombre_hijo2_sent = "";
  184.     }
  185.     if(isset($_POST['fecha_nombre_hijo2']) && $_POST['fecha_nombre_hijo2'] != "")
  186.     {
  187.         $fecha_nombre_hijo2_sent = $_POST['fecha_nombre_hijo2'];
  188.     }elseif(!isset($_POST['fecha_nombre_hijo2'])){
  189.         $fecha_nombre_hijo2_sent = "";
  190.     }
  191.     if(isset($_POST['nombre_hijo3']) && $_POST['nombre_hijo3'] != "")
  192.     {
  193.         $nombre_hijo3_sent = $_POST['nombre_hijo3'];
  194.     }elseif(!isset($_POST['nombre_hijo3'])){
  195.         $nombre_hijo3_sent = "";
  196.     }
  197.     if(isset($_POST['fecha_nombre_hijo3']) && $_POST['fecha_nombre_hijo3'] != "")
  198.     {
  199.         $fecha_nombre_hijo3_sent = $_POST['fecha_nombre_hijo3'];
  200.     }elseif(!isset($_POST['fecha_nombre_hijo3'])){
  201.         $fecha_nombre_hijo3_sent = "";
  202.     }
  203.     if(isset($_POST['nombre_hijo4']) && $_POST['nombre_hijo4'] != "")
  204.     {
  205.         $nombre_hijo4_sent = $_POST['nombre_hijo4'];
  206.     }elseif(!isset($_POST['nombre_hijo4'])){
  207.         $nombre_hijo4_sent = "";
  208.     }
  209.     if(isset($_POST['fecha_nombre_hijo4']) && $_POST['fecha_nombre_hijo4'] != "")
  210.     {
  211.         $fecha_nombre_hijo4_sent = $_POST['fecha_nombre_hijo4'];
  212.     }elseif(!isset($_POST['fecha_nombre_hijo4'])){
  213.         $fecha_nombre_hijo4_sent = "";
  214.     }
  215.     if(isset($_POST['nombre_hijo5']) && $_POST['nombre_hijo5'] != "")
  216.     {
  217.         $nombre_hijo5_sent = $_POST['nombre_hijo5'];
  218.     }elseif(!isset($_POST['nombre_hijo5'])){
  219.         $nombre_hijo5_sent = "";
  220.     }
  221.     if(isset($_POST['fecha_nombre_hijo5']) && $_POST['fecha_nombre_hijo5'] != "")
  222.     {
  223.         $fecha_nombre_hijo5_sent = $_POST['fecha_nombre_hijo5'];
  224.     }elseif(!isset($_POST['fecha_nombre_hijo5'])){
  225.         $fecha_nombre_hijo5_sent = "";
  226.     }
  227.    
  228.     header("Location: solicitar_usuario.php?con todas las variables definidas");
  229. }

Este es para direccionar, luego vuelvo a definir las variables para insertar

Código PHP:
Ver original
  1. #igualo variables para inserción
  2. $nombre = $_POST['nombre'];
  3. $cedula = $_POST['cedula'];
  4. #valido los campos opcionales
  5. if(isset($_POST['telefono2']) && $_POST['telefono2'] != ""){ $telefono2 = $_POST['telefono2'];}elseif(!isset($_POST['telefono2'])){ $telefono2 = "sin datos";}
  6.  
  7. #.....(son muchos :( no pongo todos )

Y por último hago la inserción:

Código PHP:
Ver original
  1. mysql_select_db("helponline",$connected);
  2.         $sql = "
  3.         INSERT INTO `helponline`.`solicitud_usuario` (
  4.             `id_solicitud` ,
  5.             `password` ,
  6.             `usuario` ,
  7.             `y todo lo demás!'
  8.             )
  9.             VALUES (NULL , '$codigo_humano', '$usuario', 'y todo lo demás!')";
  10.        
  11.         mysql_query($sql) or die(mysql_error());
  12.        
  13.         header("Location: insertar_solicitud.php?registro=ok");

Luego arriba tengo una condicion que si existe $_GET registro = ok me muestra un html, en esa parte funciona bien.

No tengo idea de que estoy haciendo mal, porque al existir un campo vacío me tira de nuevo a solicitud_usuario com las variables perfectas. pero igual me inserta en la DB

Talvez alguien me pueda ayudar!

saludos

PD: recorte el código por problemas de límites de letras del foro.
__________________
--
What is real? How do you define real?
If real is what you can feel, smell, taste and see, then real is simply electrical signals interpreted by your brain.
  #2 (permalink)  
Antiguo 05/08/2012, 13:30
Avatar de Nemutagk
Colaborador
 
Fecha de Ingreso: marzo-2004
Ubicación: México
Mensajes: 2.633
Antigüedad: 20 años
Puntos: 406
Respuesta: Al raro con header oO

Eso pasa por no leer como funciona header en el manual, cuando haces un header:location PHP sigue ejecutando el código que hay después hasta terminar de ejecutar todo el script, por eso, después de hacer la redirección se agrega un exit();
__________________
Listo?, tendría que tener 60 puntos menos de IQ para considerarme listo!!!
-- Sheldon Cooper
http://twitter.com/nemutagk
PD: No contestaré temas vía mensaje personal =)
  #3 (permalink)  
Antiguo 05/08/2012, 14:37
Avatar de KsrZ  
Fecha de Ingreso: abril-2011
Ubicación: /home/KsrZ/Desktop
Mensajes: 156
Antigüedad: 13 años
Puntos: 26
Respuesta: Al raro con header oO

fuera de tu error.. creo, la mayoria de las interfaces de mysql dan error :
con '' indicas campos tablas bases de datos..
con `` indicas valores..
caso contrario toma '' del campo actual llenándolo como tu valor.. si no existe, lo da como vacio

ej:..
esto:
VALUES (NULL , '$codigo_humano', '$usuario', 'y todo lo demás!')
seria equivalente a:
VALUES ( , valor = campo actual, valor = campo actual, valor = campo actual)

yo en mi caso usaria.. :
VALUES (`` , `$codigo_humano`, `$usuario`,`y todo lo demás!`)

seria equivalente a:
VALUES ( , valor indicado, valor indicado, valor indicado)

Última edición por KsrZ; 05/08/2012 a las 14:43
  #4 (permalink)  
Antiguo 06/08/2012, 18:01
 
Fecha de Ingreso: julio-2012
Ubicación: Asunción
Mensajes: 54
Antigüedad: 11 años, 9 meses
Puntos: 3
Respuesta: Al raro con header oO

Excelente muchas gracias por las dos respuestas.
pondré el exit y tambien editaré la parte de los valores.

+1 para ambas respuestas
__________________
--
What is real? How do you define real?
If real is what you can feel, smell, taste and see, then real is simply electrical signals interpreted by your brain.

Etiquetas: header:location, sql
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 01:12.