Foros del Web » Programando para Internet » PHP »

[SOLUCIONADO] Consulta de Campos en tablas.

Estas en el tema de Consulta de Campos en tablas. en el foro de PHP en Foros del Web. Hola tengo una duda y es que en el proyecto que estoy haciendo tengo un formulario en el que ingreso datos de clientes y estos ...

  #1 (permalink)  
Antiguo 21/01/2013, 09:48
Avatar de satjaen  
Fecha de Ingreso: septiembre-2012
Ubicación: Jaén (Andalucía)
Mensajes: 893
Antigüedad: 11 años, 7 meses
Puntos: 10
Consulta de Campos en tablas.

Hola tengo una duda y es que en el proyecto que estoy haciendo tengo un formulario en el que ingreso datos de clientes y estos a su vez pueden tener varias direcciones.
Es decir:
tabla clientes
1 -Juan Parras
2-Pedro Carrasco

tabla direcciones
el cliente 1 -Juan Parras puede tener 3 dirrecciones distintas:
1- Calle -Avd Andalucia
2- Calle - Extremadura
3- Calle- Rosario de la Aurora

Ahora bien si yo en el formulario del cliente cuando ingreso los datos:
1º- ¿Puedo ingresar las 3 direcciones del tiron en la tabla direcciones?


Un saludo
  #2 (permalink)  
Antiguo 21/01/2013, 10:58
Avatar de GatorV
$this->role('moderador');
 
Fecha de Ingreso: mayo-2006
Ubicación: /home/ams/
Mensajes: 38.567
Antigüedad: 17 años, 11 meses
Puntos: 2135
Respuesta: Consulta de Campos en tablas.

Claro que es posible, tienes que usar algo dinámico como Javascript para agregar los campos, pero en PHP recibes todos tus campos si usas la nomenclatura de array, ej:
Código HTML:
Ver original
  1. <input type="text" name="nombres[]">

En PHP recibirías un array con todos los nombres.

Saludos.
  #3 (permalink)  
Antiguo 21/01/2013, 11:50
Avatar de satjaen  
Fecha de Ingreso: septiembre-2012
Ubicación: Jaén (Andalucía)
Mensajes: 893
Antigüedad: 11 años, 7 meses
Puntos: 10
Respuesta: Consulta de Campos en tablas.

Cita:
Iniciado por GatorV Ver Mensaje
Claro que es posible, tienes que usar algo dinámico como Javascript para agregar los campos, pero en PHP recibes todos tus campos si usas la nomenclatura de array, ej:
Código HTML:
Ver original
  1. <input type="text" name="nombres[]">

En PHP recibirías un array con todos los nombres.

Saludos.
Gracias GatorV, pero soy novato. Me puedes desarrollar un poco mas o poner un ejemplo pequeño?
Gracias
  #4 (permalink)  
Antiguo 21/01/2013, 12:29
Avatar de GatorV
$this->role('moderador');
 
Fecha de Ingreso: mayo-2006
Ubicación: /home/ams/
Mensajes: 38.567
Antigüedad: 17 años, 11 meses
Puntos: 2135
Respuesta: Consulta de Campos en tablas.

Pues simplemente es que tus inputs tengan nomenclatura de array y en PHP los puedes rescatar, revisa el siguiente link: http://www.php.net/variables.external

Saludos.
  #5 (permalink)  
Antiguo 21/01/2013, 13:14
Avatar de satjaen  
Fecha de Ingreso: septiembre-2012
Ubicación: Jaén (Andalucía)
Mensajes: 893
Antigüedad: 11 años, 7 meses
Puntos: 10
Respuesta: Consulta de Campos en tablas.

Cita:
Iniciado por GatorV Ver Mensaje
Pues simplemente es que tus inputs tengan nomenclatura de array y en PHP los puedes rescatar, revisa el siguiente link: http://www.php.net/variables.external

Saludos.
Ok, gracias. Me pongo con ello y te comento.
  #6 (permalink)  
Antiguo 21/01/2013, 14:30
Avatar de satjaen  
Fecha de Ingreso: septiembre-2012
Ubicación: Jaén (Andalucía)
Mensajes: 893
Antigüedad: 11 años, 7 meses
Puntos: 10
Respuesta: Consulta de Campos en tablas.

He puesto un NUM_DIR para identificar cada dirección:
Código:
 <div id="TabbedPanels2" class="TabbedPanels">
            <ul class="TabbedPanelsTabGroup">
              <li class="TabbedPanelsTab" tabindex="0">Dirección</li>
              <li class="TabbedPanelsTab" tabindex="0">Dirección 2</li>
              <li class="TabbedPanelsTab" tabindex="0">Dirección 3</li>
            </ul>
            <div class="TabbedPanelsContentGroup">
              <div class="TabbedPanelsContent">
              <table width="100%">
                  <tr>
                    <th scope="col">Calle</th>
                    <input name="NUM_DIR" type="hidden" size="" value="1" />
                    <th scope="col"><input name="calle" type="text" size="30"value="" /></th>
                    <th scope="col">Nº</th>
                    <th scope="col"><input name="numero" type="text" size="1"value="" />
                    EDF
                    <input name="edificio" type="text" size="1"value="" />
                    PTA
                    <input name="puerta" type="text" size="1"value="" />
                    PISO
                    <input name="piso" type="text" size="1"value="" />
                    LTA
                    <input name="letra" type="text" size="1"value="" /></th>
                </tr>
                     <tr>
                    <th scope="col">Localidad</th>
                    <th scope="col"><input name="poblacion" type="text" size="30" value="" /></th>
                    <th scope="col">Provincia</th>
                    <th scope="col"><input name="provincia" type="text" size="30" value="" /></th>
                  </tr>
                  <tr>
                  <th scope="col">Email</th>
                    <th scope="col"><input name="email" type="text" size="30" value=""/></th>
                </tr>
              </table>
              </div>
              <div class="TabbedPanelsContent"> 
               <table width="100%">
                  <tr>
                    <th scope="col">Calle</th>
                    <input name="NUM_DIR" type="hidden" size="" value="2" />
                    <th scope="col"><input name="calle" type="text" size="30"value="" /></th>
                    <th scope="col">Nº</th>
                    <th scope="col"><input name="numero" type="text" size="1"value="" />
                    EDF
                    <input name="edificio" type="text" size="1"value="" />
                    PTA
                    <input name="puerta" type="text" size="1"value="" />
                    PISO
                    <input name="piso" type="text" size="1"value="" />
                    LTA
                    <input name="letra" type="text" size="1"value="" /></th>
                </tr>
                     <tr>
                    <th scope="col">Localidad</th>
                    <th scope="col"><input name="poblacion" type="text" size="30" value="" /></th>
                    <th scope="col">Provincia</th>
                    <th scope="col"><input name="provincia" type="text" size="30" value="" /></th>
                  </tr>
                  <tr>
                  <th scope="col">Email</th>
                    <th scope="col"><input name="email" type="text" size="30" value=""/></th>
                </tr>
              </table>
              </div>
              <div class="TabbedPanelsContent"> 
               <table width="100%">
                  <tr>
                    <th scope="col">Calle</th>
                    <input name="NUM_DIR" type="hidden" size="" value="3" />
                    <th scope="col"><input name="calle" type="text" size="30"value="" /></th>
                    <th scope="col">Nº</th>
                    <th scope="col"><input name="numero" type="text" size="1"value="" />
                    EDF
                    <input name="edificio" type="text" size="1"value="" />
                    PTA
                    <input name="puerta" type="text" size="1"value="" />
                    PISO
                    <input name="piso" type="text" size="1"value="" />
                    LTA
                    <input name="letra" type="text" size="1"value="" /></th>
                </tr>
                     <tr>
                    <th scope="col">Localidad</th>
                    <th scope="col"><input name="poblacion" type="text" size="30" value="" /></th>
                    <th scope="col">Provincia</th>
                    <th scope="col"><input name="provincia" type="text" size="30" value="" /></th>
                  </tr>
                  <tr>
                  <th scope="col">Email</th>
                    <th scope="col"><input name="email" type="text" size="30" value=""/></th>
                </tr>
              </table>
              </div>
Pero claro al hacer el INSERT solo me insertaría una dirección no?

Código PHP:
$insertSQL sprintf("INSERT INTO dir_usuarios (id_aviso,calle,numero,edificio,puerta,piso,letra,localidad,provincia,NUM_DIR)VALUES (%s, %s, %s, %s, %s, %s,%s, %s, %s, %s)",
                        
GetSQLValueString($_POST['id_aviso'], "int"),
                       
GetSQLValueString($_POST['calle'], "text"),
                       
GetSQLValueString($_POST['numero'], "text"),
                       
GetSQLValueString($_POST['edificio'], "text"),
                       
GetSQLValueString($_POST['puerta'], "text"),
                       
GetSQLValueString($_POST['piso'], "text"),
                       
GetSQLValueString($_POST['letra'], "text"),
                       
GetSQLValueString($_POST['localidad'], "text"),
                       
GetSQLValueString($_POST['provincia'], "text"),
                       
GetSQLValueString($_POST['NUM_DIR'], "text"));

  
mysql_select_db($database_conexion$conexion);
  
$usuarios mysql_query($insertSQL$conexion) or die(mysql_error()); 
  #7 (permalink)  
Antiguo 21/01/2013, 15:08
Avatar de GatorV
$this->role('moderador');
 
Fecha de Ingreso: mayo-2006
Ubicación: /home/ams/
Mensajes: 38.567
Antigüedad: 17 años, 11 meses
Puntos: 2135
Respuesta: Consulta de Campos en tablas.

Como te comenté más arriba, tienes que usar nomenclatura de arrays, para eso es obligatorio usar [] para que al recibirlo en PHP tengas un array, de lo contrario solo se va a enviar el último valor con ese nombre.

Saludos.
  #8 (permalink)  
Antiguo 21/01/2013, 15:24
Avatar de satjaen  
Fecha de Ingreso: septiembre-2012
Ubicación: Jaén (Andalucía)
Mensajes: 893
Antigüedad: 11 años, 7 meses
Puntos: 10
Respuesta: Consulta de Campos en tablas.

Cita:
Iniciado por GatorV Ver Mensaje
Como te comenté más arriba, tienes que usar nomenclatura de arrays, para eso es obligatorio usar [] para que al recibirlo en PHP tengas un array, de lo contrario solo se va a enviar el último valor con ese nombre.

Saludos.
Te refieres que le ponga a todos:

Código PHP:
<input name="calle[]" type="text" size="30"value="" /> 
Pero entonces tengo que modificar el INSERT y como lo recibo en php.
  #9 (permalink)  
Antiguo 21/01/2013, 15:48
Avatar de satjaen  
Fecha de Ingreso: septiembre-2012
Ubicación: Jaén (Andalucía)
Mensajes: 893
Antigüedad: 11 años, 7 meses
Puntos: 10
Respuesta: Consulta de Campos en tablas.

Perdón quería decir así:

Código PHP:
<input name="calle[1]" type="text" size="30"value="" />
<
input name="calle[2]" type="text" size="30"value="" />
<
input name="calle[3]" type="text" size="30"value="" /> 
Pero entonces tengo que modificar el INSERT y como lo recibo en php.
  #10 (permalink)  
Antiguo 21/01/2013, 15:56
Avatar de GatorV
$this->role('moderador');
 
Fecha de Ingreso: mayo-2006
Ubicación: /home/ams/
Mensajes: 38.567
Antigüedad: 17 años, 11 meses
Puntos: 2135
Respuesta: Consulta de Campos en tablas.

No es necesario que le pongas el índice, con que lo pongas como [] es suficiente, en PHP lo recibes usando ya sea $_POST o $_GET y es un arreglo que puedes iterar usando un for() o un foreach().

Saludos.
  #11 (permalink)  
Antiguo 21/01/2013, 16:04
Avatar de satjaen  
Fecha de Ingreso: septiembre-2012
Ubicación: Jaén (Andalucía)
Mensajes: 893
Antigüedad: 11 años, 7 meses
Puntos: 10
Respuesta: Consulta de Campos en tablas.

Cita:
Iniciado por GatorV Ver Mensaje
No es necesario que le pongas el índice, con que lo pongas como [] es suficiente, en PHP lo recibes usando ya sea $_POST o $_GET y es un arreglo que puedes iterar usando un for() o un foreach().

Saludos.
Quieres decir así:

Código PHP:
<div id="TabbedPanels2" class="TabbedPanels">
            <
ul class="TabbedPanelsTabGroup">
              <
li class="TabbedPanelsTab" tabindex="0">Dirección</li>
              <
li class="TabbedPanelsTab" tabindex="0">Dirección 2</li>
              <
li class="TabbedPanelsTab" tabindex="0">Dirección 3</li>
            </
ul>
            <
div class="TabbedPanelsContentGroup">
              <
div class="TabbedPanelsContent">
              <
table width="100%">
                  <
tr>
                    <
th scope="col">Calle</th>
                    <
input name="NUM_DIR[]" type="hidden" size="" value="1" />
                    <
th scope="col"><input name="calle[]" type="text" size="30"value="" /></th>
                    <
th scope="col"></th>
                    <
th scope="col"><input name="numero[]" type="text" size="1"value="" />
                    
EDF
                    
<input name="edificio[]" type="text" size="1"value="" />
                    
PTA
                    
<input name="puerta[]" type="text" size="1"value="" />
                    
PISO
                    
<input name="piso[]" type="text" size="1"value="" />
                    
LTA
                    
<input name="letra[]" type="text" size="1"value="" /></th>
                </
tr>
                     <
tr>
                    <
th scope="col">Localidad</th>
                    <
th scope="col"><input name="poblacion[]" type="text" size="30" value="" /></th>
                    <
th scope="col">Provincia</th>
                    <
th scope="col"><input name="provincia[]" type="text" size="30" value="" /></th>
                  </
tr>
                  <
tr>
                  <
th scope="col">Email</th>
                    <
th scope="col"><input name="email[]" type="text" size="30" value=""/></th>
                </
tr>
              </
table>
              </
div>
              <
div class="TabbedPanelsContent"
               <
table width="100%">
                  <
tr>
                    <
th scope="col">Calle</th>
                    <
input name="NUM_DIR[]" type="hidden" size="" value="2" />
                    <
th scope="col"><input name="calle[]" type="text" size="30"value="" /></th>
                    <
th scope="col"></th>
                    <
th scope="col"><input name="numero[]" type="text" size="1"value="" />
                    
EDF
                    
<input name="edificio[]" type="text" size="1"value="" />
                    
PTA
                    
<input name="puerta[]" type="text" size="1"value="" />
                    
PISO
                    
<input name="piso[]" type="text" size="1"value="" />
                    
LTA
                    
<input name="letra[]" type="text" size="1"value="" /></th>
                </
tr>
                     <
tr>
                    <
th scope="col">Localidad</th>
                    <
th scope="col"><input name="poblacion[]" type="text" size="30" value="" /></th>
                    <
th scope="col">Provincia</th>
                    <
th scope="col"><input name="provincia[]" type="text" size="30" value="" /></th>
                  </
tr>
                  <
tr>
                  <
th scope="col">Email</th>
                    <
th scope="col"><input name="email[]" type="text" size="30" value=""/></th>
                </
tr>
              </
table>
              </
div>
              <
div class="TabbedPanelsContent"
               <
table width="100%">
                  <
tr>
                    <
th scope="col">Calle</th>
                    <
input name="NUM_DIR[]" type="hidden" size="" value="3" />
                    <
th scope="col"><input name="calle[]" type="text" size="30"value="" /></th>
                    <
th scope="col"></th>
                    <
th scope="col"><input name="numero[]" type="text" size="1"value="" />
                    
EDF
                    
<input name="edificio[]" type="text" size="1"value="" />
                    
PTA
                    
<input name="puerta[]" type="text" size="1"value="" />
                    
PISO
                    
<input name="piso[]" type="text" size="1"value="" />
                    
LTA
                    
<input name="letra[]" type="text" size="1"value="" /></th>
                </
tr>
                     <
tr>
                    <
th scope="col">Localidad</th>
                    <
th scope="col"><input name="poblacion[]" type="text" size="30" value="" /></th>
                    <
th scope="col">Provincia</th>
                    <
th scope="col"><input name="provincia[]" type="text" size="30" value="" /></th>
                  </
tr>
                  <
tr>
                  <
th scope="col">Email</th>
                    <
th scope="col"><input name="email[]" type="text" size="30" value=""/></th>
                </
tr>
              </
table>
              </
div
Pero y el INSERT me va a ingresar solo una dirección no?
  #12 (permalink)  
Antiguo 21/01/2013, 16:05
Avatar de GatorV
$this->role('moderador');
 
Fecha de Ingreso: mayo-2006
Ubicación: /home/ams/
Mensajes: 38.567
Antigüedad: 17 años, 11 meses
Puntos: 2135
Respuesta: Consulta de Campos en tablas.

Claro, tienes que modificar tu insert y hacerlo "n" veces por cada valor del arreglo que tengas, así puedes hacer las inserciones que quieras.

Saludos.
  #13 (permalink)  
Antiguo 21/01/2013, 16:07
Avatar de satjaen  
Fecha de Ingreso: septiembre-2012
Ubicación: Jaén (Andalucía)
Mensajes: 893
Antigüedad: 11 años, 7 meses
Puntos: 10
Respuesta: Consulta de Campos en tablas.

Cita:
Iniciado por GatorV Ver Mensaje
Claro, tienes que modificar tu insert y hacerlo "n" veces por cada valor del arreglo que tengas, así puedes hacer las inserciones que quieras.

Saludos.
Perdona, pero no lo entiendo.
  #14 (permalink)  
Antiguo 21/01/2013, 16:20
Avatar de GatorV
$this->role('moderador');
 
Fecha de Ingreso: mayo-2006
Ubicación: /home/ams/
Mensajes: 38.567
Antigüedad: 17 años, 11 meses
Puntos: 2135
Respuesta: Consulta de Campos en tablas.

Pues simple:
Código HTML:
Ver original
  1. <input type="text" name="nombres[]">
  2. <input type="text" name="nombres[]">
  3. <input type="text" name="nombres[]">

PHP:
Código PHP:
Ver original
  1. <?php
  2. $nombres = $_POST['nombres'];
  3. foreach ($nombres as $nombre) {
  4.       echo $nombre . "<br>";
  5. }

La idea es que veas como recibes un valor como arreglo y dentro de tu foreach() tendrías que hacer tus INSERTs.

Saludos.
  #15 (permalink)  
Antiguo 21/01/2013, 16:30
Avatar de satjaen  
Fecha de Ingreso: septiembre-2012
Ubicación: Jaén (Andalucía)
Mensajes: 893
Antigüedad: 11 años, 7 meses
Puntos: 10
Respuesta: Consulta de Campos en tablas.

Cita:
Iniciado por GatorV Ver Mensaje
Pues simple:
Código HTML:
Ver original
  1. <input type="text" name="nombres[]">
  2. <input type="text" name="nombres[]">
  3. <input type="text" name="nombres[]">

PHP:
Código PHP:
Ver original
  1. <?php
  2. $nombres = $_POST['nombres'];
  3. foreach ($nombres as $nombre) {
  4.       echo $nombre . "<br>";
  5. }

La idea es que veas como recibes un valor como arreglo y dentro de tu foreach() tendrías que hacer tus INSERTs.

Saludos.
Gracias y perdona las molestias pero esos INSERT no se hacerlos.
  #16 (permalink)  
Antiguo 21/01/2013, 16:59
Avatar de satjaen  
Fecha de Ingreso: septiembre-2012
Ubicación: Jaén (Andalucía)
Mensajes: 893
Antigüedad: 11 años, 7 meses
Puntos: 10
Respuesta: Consulta de Campos en tablas.

Cita:
Iniciado por satjaen Ver Mensaje
Gracias y perdona las molestias pero esos INSERT no se hacerlos.
Vale pero para eso tendria que poner:

Código HTML:
Ver original
  1. <input type="text" name="calle[1]">
  2. <input type="text" name="calle[2]">
  3. <input type="text" name="calle[3]">

Y luego 3 INSERTs:

Código PHP:
Ver original
  1. <?php
  2. $nombre= $_POST['calle'];
  3. foreach ($calle as $nombre) {
  4.     insert into dir_usuarios calle values(%s);
  5. }  
  6.  
  7. $calle= $_POST['calle'];
  8. foreach ($calle as $nombre) {
  9.     insert into dir_usuarios calle values(%s);
  10. }  
  11.  
  12. $calle= $_POST['calle'];
  13. foreach ($calle as $nombre) {
  14.     insert into dir_usuarios calle values(%s);
  15. }

Y como se identifican los INSERTs y si solo quiero hacer un solo INSERT me ingresaría uno solo o me haría 3 ingresos?.
  #17 (permalink)  
Antiguo 21/01/2013, 18:38
 
Fecha de Ingreso: diciembre-2008
Mensajes: 20
Antigüedad: 15 años, 3 meses
Puntos: 3
Respuesta: Consulta de Campos en tablas.

no amigo, el foreach significa "por cada"...
solo necesitas un foreach

Ejemplo comentado:

Código HTML:
<input type="text" name="calle[1]">
<input type="text" name="calle[2]">
<input type="text" name="calle[3]"> 
Código PHP:
<?php
// Esto dice: que la variable $calles guarda todos los arrays de los inputs "calle" 
$calles$_POST['calle'];
//Por cada $calles un $calle, entonces son tres $calles, hará tres ciclos, en cada ciclo $calle, aumentara el indice del array
foreach ($calles as $calle) {
// en cada ciclo insertara en dir_usuarios el valor de $calle[n],  y asi tantos $calles tengas, en este caso tres
    
insert into dir_usuarios $calle values (%s);
}

?>
PD: No se mucho de php pero se hace lo que se puede... si estoy mal en la sintaxis o algo, ahi me corrigen los mas grandes, pero esto es como para que le entiendas mas o menos... jeje asi es como yo lo entiendo.



Ejemplo: mas limpio


Código HTML:
<!-- Como dice GatorV no es necesario ponerle los indices -->
<input type="text" name="calle[]" value="calle numero uno">
<input type="text" name="calle[]" value="calle numero dos">
<input type="text" name="calle[]" value="calle numero tres"> 

Código PHP:
<?php
$calles
$_POST['calle'];
foreach (
$calles as $calle) {
  echo 
$calle."<br>";
}

?>

Este codigo resultaria en esto:
Código HTML:
Calle numero uno
Calle numero dos
Calle numero tres

Última edición por ito07; 21/01/2013 a las 18:50
  #18 (permalink)  
Antiguo 21/01/2013, 18:52
Avatar de satjaen  
Fecha de Ingreso: septiembre-2012
Ubicación: Jaén (Andalucía)
Mensajes: 893
Antigüedad: 11 años, 7 meses
Puntos: 10
Respuesta: Consulta de Campos en tablas.

Cita:
Iniciado por ito07 Ver Mensaje
no amigo, el foreach significa "por cada"...
solo necesitas un foreach

Ejemplo comentado:

Código HTML:
<input type="text" name="calle[1]">
<input type="text" name="calle[2]">
<input type="text" name="calle[3]"> 
Código PHP:
<?php
// Esto dice: que la variable $calles guarda todos los arrays de los inputs "calle" 
$calles$_POST['calle'];
//Por cada $calles un $calle, entonces son tres $calles, hará tres ciclos, en cada ciclo $calle, aumentara el indice del array
foreach ($calles as $calle) {
// en cada ciclo insertara en dir_usuarios el valor de $calle[n],  y asi tantos $calles tengas, en este caso tres
    
insert into dir_usuarios $calle values (%s);
}

?>
PD: No se mucho de php pero se hace lo que se puede... si estoy mal en la sintaxis o algo, ahi me corrigen los mas grandes, pero esto es como para que le entiendas mas o menos... jeje asi es como yo lo entiendo.



Ejemplo: mas limpio


Código HTML:
<input type="text" name="calle[1]" value="calle numero uno">
<input type="text" name="calle[2]" value="calle numero dos">
<input type="text" name="calle[3]" value="calle numero tres"> 

Código PHP:
<?php
$calles
$_POST['calle'];
foreach (
$calles as $calle) {
  echo 
$calle."<br>";
}

?>

Este codigo resultaria en esto:
Código HTML:
Calle numero uno
Calle numero dos
Calle numero tres

Ok, muchas gracias y en los demas datos como localidad, etc.. tendría que hacer otros INSERTs?
  #19 (permalink)  
Antiguo 21/01/2013, 18:59
 
Fecha de Ingreso: diciembre-2008
Mensajes: 20
Antigüedad: 15 años, 3 meses
Puntos: 3
Respuesta: Consulta de Campos en tablas.

Si tienes el mismo numero de calles como de localidad los inserts los puedes incluir en el mismo foreach, de lo contrario necesitaras hacer otro foreach
  #20 (permalink)  
Antiguo 21/01/2013, 19:05
Avatar de satjaen  
Fecha de Ingreso: septiembre-2012
Ubicación: Jaén (Andalucía)
Mensajes: 893
Antigüedad: 11 años, 7 meses
Puntos: 10
Respuesta: Consulta de Campos en tablas.

Cita:
Iniciado por ito07 Ver Mensaje
Si tienes el mismo numero de calles como de localidad los inserts los puedes incluir en el mismo foreach, de lo contrario necesitaras hacer otro foreach
Si, como ves mas arriba en el form tienen todos los campos tres direcciones. Como seguiria el foreach que me has construido con algún campo mas?
Gracias por tu tiempo.
  #21 (permalink)  
Antiguo 21/01/2013, 21:59
Avatar de GatorV
$this->role('moderador');
 
Fecha de Ingreso: mayo-2006
Ubicación: /home/ams/
Mensajes: 38.567
Antigüedad: 17 años, 11 meses
Puntos: 2135
Respuesta: Consulta de Campos en tablas.

Puedes usar un for normal, por ejemplo:
Código PHP:
Ver original
  1. $nombres = $_POST['nombres'];
  2. $apellidos = $_POST['apellidos'];
  3. $edades = $_POST['edades'];
  4.  
  5. $total = count($nombres);
  6. for ($i = 0; $i < $total; $i++) {
  7.         $nombre = $nombres[$i];
  8.         $apellido = $apellidos[$i];
  9.         $edad = $edades[$i];
  10.  
  11.        echo "INSERT INTO personas VALUES ('$nombre', '$apellido', '$edad')";
  12. }

Saludos.
  #22 (permalink)  
Antiguo 22/01/2013, 03:11
Avatar de satjaen  
Fecha de Ingreso: septiembre-2012
Ubicación: Jaén (Andalucía)
Mensajes: 893
Antigüedad: 11 años, 7 meses
Puntos: 10
Respuesta: Consulta de Campos en tablas.

Cita:
Iniciado por GatorV Ver Mensaje
Puedes usar un for normal, por ejemplo:
Código PHP:
Ver original
  1. $nombres = $_POST['nombres'];
  2. $apellidos = $_POST['apellidos'];
  3. $edades = $_POST['edades'];
  4.  
  5. $total = count($nombres);
  6. for ($i = 0; $i < $total; $i++) {
  7.         $nombre = $nombres[$i];
  8.         $apellido = $apellidos[$i];
  9.         $edad = $edades[$i];
  10.  
  11.        echo "INSERT INTO personas VALUES ('$nombre', '$apellido', '$edad')";
  12. }

Saludos.
No se si estara bien?
Una cosa así:

Código PHP:
Ver original
  1. }
  2. $id_aviso=$_POST['id_aviso'];
  3. $poblacion = $_POST['poblacion'];
  4. $provincia= $_POST['provincia'];
  5. $calle = $_POST['calle'];
  6. $numero = $_POST['numero'];
  7. $edificio= $_POST['edificio'];
  8. $puerta = $_POST['puerta'];
  9. $piso = $_POST['piso'];
  10. $letra= $_POST['letra'];
  11. $email = $_POST['email'];
  12. $NUM_DIR = $_POST['NUM_DIR'];
  13.  
  14. $total = count($poblacion);
  15. for ($i = 0; $i < $total; $i++) {
  16.         $poblacion = $poblacion[$i];
  17.         $provincia = $provincia[$i];
  18.         $calle = $calle[$i];
  19.         $numero = $numero[$i];
  20.         $edificio = $edificio[$i];
  21.         $puerta = $puerta[$i];
  22.         $piso = $piso[$i];
  23.         $letra = $letra[$i];
  24.         $email = $email[$i];
  25.         $NUM_DIR = $NUM_DIR[$i];
  26.  
  27.        echo "INSERT INTO dir_usuarios VALUES (id_aviso,'$poblacion', '$provincia', '$calle','$numero', '$edificio', '$puerta','$piso', '$letra', '$email', '$NUM_DIR')";
  28. }

Última edición por satjaen; 22/01/2013 a las 04:22
  #23 (permalink)  
Antiguo 22/01/2013, 04:38
Avatar de satjaen  
Fecha de Ingreso: septiembre-2012
Ubicación: Jaén (Andalucía)
Mensajes: 893
Antigüedad: 11 años, 7 meses
Puntos: 10
Respuesta: Consulta de Campos en tablas.

Lo estoy haciendo así, pero no me inserta nada:

Código HTML:
Ver original
  1. <div id="TabbedPanels2" class="TabbedPanels">
  2.             <ul class="TabbedPanelsTabGroup">
  3.               <li class="TabbedPanelsTab" tabindex="0">Dirección</li>
  4.               <li class="TabbedPanelsTab" tabindex="0">Dirección 2</li>
  5.               <li class="TabbedPanelsTab" tabindex="0">Dirección 3</li>
  6.             </ul>
  7.             <div class="TabbedPanelsContentGroup">
  8.               <div class="TabbedPanelsContent">
  9.               <table width="100%">
  10.                   <tr>
  11.                     <th scope="col">Calle</th>
  12.                     <input name="NUM_DIR[]" type="hidden" size="" value="1" />
  13.                     <th scope="col"><input name="calle[]" type="text" size="30"value="" /></th>
  14.                     <th scope="col"></th>
  15.                     <th scope="col"><input name="numero[]" type="text" size="1"value="" />
  16.                     EDF
  17.                     <input name="edificio[]" type="text" size="1"value="" />
  18.                     PTA
  19.                     <input name="puerta[]" type="text" size="1"value="" />
  20.                     PISO
  21.                     <input name="piso[]" type="text" size="1"value="" />
  22.                     LTA
  23.                     <input name="letra[]" type="text" size="1"value="" /></th>
  24.                 </tr>
  25.                      <tr>
  26.                     <th scope="col">Localidad</th>
  27.                     <th scope="col"><input name="poblacion[]" type="text" size="30" value="" /></th>
  28.                     <th scope="col">Provincia</th>
  29.                     <th scope="col"><input name="provincia[]" type="text" size="30" value="" /></th>
  30.                   </tr>
  31.                   <tr>
  32.                   <th scope="col">Email</th>
  33.                     <th scope="col"><input name="email[]" type="text" size="30" value=""/></th>
  34.                 </tr>
  35.               </table>
  36.               </div>
  37.               <div class="TabbedPanelsContent">
  38.                <table width="100%">
  39.                   <tr>
  40.                     <th scope="col">Calle</th>
  41.                     <input name="NUM_DIR[]" type="hidden" size="" value="2" />
  42.                     <th scope="col"><input name="calle[]" type="text" size="30"value="" /></th>
  43.                     <th scope="col"></th>
  44.                     <th scope="col"><input name="numero[]" type="text" size="1"value="" />
  45.                     EDF
  46.                     <input name="edificio[]" type="text" size="1"value="" />
  47.                     PTA
  48.                     <input name="puerta[]" type="text" size="1"value="" />
  49.                     PISO
  50.                     <input name="piso[]" type="text" size="1"value="" />
  51.                     LTA
  52.                     <input name="letra[]" type="text" size="1"value="" /></th>
  53.                 </tr>
  54.                      <tr>
  55.                     <th scope="col">Localidad</th>
  56.                     <th scope="col"><input name="poblacion[]" type="text" size="30" value="" /></th>
  57.                     <th scope="col">Provincia</th>
  58.                     <th scope="col"><input name="provincia[]" type="text" size="30" value="" /></th>
  59.                   </tr>
  60.                   <tr>
  61.                   <th scope="col">Email</th>
  62.                     <th scope="col"><input name="email[]" type="text" size="30" value=""/></th>
  63.                 </tr>
  64.               </table>
  65.               </div>
  66.               <div class="TabbedPanelsContent">
  67.                <table width="100%">
  68.                   <tr>
  69.                     <th scope="col">Calle</th>
  70.                     <input name="NUM_DIR[]" type="hidden" size="" value="3" />
  71.                     <th scope="col"><input name="calle[]" type="text" size="30"value="" /></th>
  72.                     <th scope="col"></th>
  73.                     <th scope="col"><input name="numero[]" type="text" size="1"value="" />
  74.                     EDF
  75.                     <input name="edificio[]" type="text" size="1"value="" />
  76.                     PTA
  77.                     <input name="puerta[]" type="text" size="1"value="" />
  78.                     PISO
  79.                     <input name="piso[]" type="text" size="1"value="" />
  80.                     LTA
  81.                     <input name="letra[]" type="text" size="1"value="" /></th>
  82.                 </tr>
  83.                      <tr>
  84.                     <th scope="col">Localidad</th>
  85.                     <th scope="col"><input name="poblacion[]" type="text" size="30" value="" /></th>
  86.                     <th scope="col">Provincia</th>
  87.                     <th scope="col"><input name="provincia[]" type="text" size="30" value="" /></th>
  88.                   </tr>
  89.                   <tr>
  90.                   <th scope="col">Email</th>
  91.                     <th scope="col"><input name="email[]" type="text" size="30" value=""/></th>
  92.                 </tr>
  93.               </table>
  94.               </div>

Código PHP:

<?php require_once('Connections/conexion.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function 
GetSQLValueString($theValue$theType$theDefinedValue ""$theNotDefinedValue ""
{
  if (
PHP_VERSION 6) {
    
$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']);
}
$id_aviso=$_POST['id_aviso'];
$poblacion $_POST['poblacion'];
$provincia$_POST['provincia'];
$calle $_POST['calle'];
$numero $_POST['numero'];
$edificio$_POST['edificio'];
$puerta $_POST['puerta'];
$piso $_POST['piso'];
$letra$_POST['letra'];
$email $_POST['email'];
$NUM_DIR $_POST['NUM_DIR'];

$total count($poblacion);
for (
$i 0$i $total$i++) {
        
$poblacion $poblacion[$i];
        
$provincia $provincia[$i];
        
$calle $calle[$i];
        
$numero $numero[$i];
        
$edificio $edificio[$i];
        
$puerta $puerta[$i];
        
$piso $piso[$i];
        
$letra $letra[$i];
        
$email $email[$i];
        
$NUM_DIR $NUM_DIR[$i];
 
       echo 
"INSERT INTO dir_usuarios VALUES (id_aviso,'$poblacion', '$provincia', '$calle','$numero', '$edificio', '$puerta','$piso', '$letra', '$email', '$NUM_DIR')";
}

mysql_select_db($database_conexion$conexion);
$query_usuarios "SELECT *
FROM usuarios WHERE id_aviso='$id_aviso'"
;
$usuarios mysql_query($query_usuarios$conexion) or die(mysql_error());
$row_usuarios mysql_fetch_assoc($usuarios);
$totalRows_usuarios mysql_num_rows($usuarios);


mysql_select_db($database_conexion$conexion);
$query_Recordset2 "SELECT * FROM dir_usuarios where id_aviso='$id_aviso' AND NUM_DIR='1'";
$Recordset2 mysql_query($query_Recordset2$conexion) or die(mysql_error());
$row_Recordset2 mysql_fetch_assoc($Recordset2);
$totalRows_Recordset2 mysql_num_rows($Recordset2);


?>
  #24 (permalink)  
Antiguo 22/01/2013, 09:17
Avatar de satjaen  
Fecha de Ingreso: septiembre-2012
Ubicación: Jaén (Andalucía)
Mensajes: 893
Antigüedad: 11 años, 7 meses
Puntos: 10
Respuesta: Consulta de Campos en tablas.

Cita:
Iniciado por GatorV Ver Mensaje
Puedes usar un for normal, por ejemplo:
Código PHP:
Ver original
  1. $nombres = $_POST['nombres'];
  2. $apellidos = $_POST['apellidos'];
  3. $edades = $_POST['edades'];
  4.  
  5. $total = count($nombres);
  6. for ($i = 0; $i < $total; $i++) {
  7.         $nombre = $nombres[$i];
  8.         $apellido = $apellidos[$i];
  9.         $edad = $edades[$i];
  10.  
  11.        echo "INSERT INTO personas VALUES ('$nombre', '$apellido', '$edad')";
  12. }

Saludos.
Esto no lo entiendo, por favor alguien me puede explicar un poco.
  #25 (permalink)  
Antiguo 22/01/2013, 10:37
Avatar de GatorV
$this->role('moderador');
 
Fecha de Ingreso: mayo-2006
Ubicación: /home/ams/
Mensajes: 38.567
Antigüedad: 17 años, 11 meses
Puntos: 2135
Respuesta: Consulta de Campos en tablas.

Pues el problema es que estas sobreescribiendo tus variables, fíjate:
Código PHP:
Ver original
  1. $poblacion = $poblacion[$i];

Ahí ya perdiste la variable original, tienes que usar otro nombre de variable diferente, en el ejemplo que te di estoy usando dos variables, $nombres es un arreglo con los nombres, y $nombre es la variable local que se asigna dentro del ciclo.

Saludos.
  #26 (permalink)  
Antiguo 22/01/2013, 14:57
Avatar de satjaen  
Fecha de Ingreso: septiembre-2012
Ubicación: Jaén (Andalucía)
Mensajes: 893
Antigüedad: 11 años, 7 meses
Puntos: 10
Respuesta: Consulta de Campos en tablas.

Cita:
Iniciado por GatorV Ver Mensaje
Pues el problema es que estas sobreescribiendo tus variables, fíjate:
Código PHP:
Ver original
  1. $poblacion = $poblacion[$i];

Ahí ya perdiste la variable original, tienes que usar otro nombre de variable diferente, en el ejemplo que te di estoy usando dos variables, $nombres es un arreglo con los nombres, y $nombre es la variable local que se asigna dentro del ciclo.

Saludos.
Lo he puesto así, pero no consigo que me haga el INSERT.

Código PHP:
Ver original
  1. <?php require_once('Connections/conexion.php'); ?>
  2. <?php
  3. if (!function_exists("GetSQLValueString")) {
  4. function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
  5. {
  6.   if (PHP_VERSION < 6) {
  7.     $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  8.   }
  9.  
  10.   $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
  11.  
  12.   switch ($theType) {
  13.     case "text":
  14.       $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
  15.       break;    
  16.     case "long":
  17.     case "int":
  18.       $theValue = ($theValue != "") ? intval($theValue) : "NULL";
  19.       break;
  20.     case "double":
  21.       $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
  22.       break;
  23.     case "date":
  24.       $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
  25.       break;
  26.     case "defined":
  27.       $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
  28.       break;
  29.   }
  30.   return $theValue;
  31. }
  32. }
  33.  
  34. $editFormAction = $_SERVER['PHP_SELF'];
  35. if (isset($_SERVER['QUERY_STRING'])) {
  36.   $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
  37. }
  38. $id_aviso=$_POST['id_aviso'];
  39. $localidad = $_POST['localidad'];
  40. $provincia= $_POST['provincia'];
  41. $calle = $_POST['calle'];
  42. $numero = $_POST['numero'];
  43. $edificio= $_POST['edificio'];
  44. $puerta = $_POST['puerta'];
  45. $piso = $_POST['piso'];
  46. $letra= $_POST['letra'];
  47. $email = $_POST['email'];
  48. $NUM_DIR = $_POST['NUM_DIR'];
  49. $total = count($localidad);
  50. for ($i = 0; $i < $total; $i++) {
  51.         $local = $localidad[$i];
  52.         $prov = $provincia[$i];
  53.         $call = $calle[$i];
  54.         $nume = $numero[$i];
  55.         $edif = $edificio[$i];
  56.         $puer = $puerta[$i];
  57.         $pis = $piso[$i];
  58.         $letr = $letra[$i];
  59.         $emai = $email[$i];
  60.  
  61.        echo "INSERT INTO dir_usuarios VALUES ('$local', '$prov', '$call', '$nume', '$edif', '$puer', '$pis', '$letr', '$emai',NUM_DIR,id_aviso)";
  62. }
  63.  
  64. mysql_select_db($database_conexion, $conexion);
  65. $query_usuarios = "SELECT *
  66. FROM usuarios WHERE id_aviso='$id_aviso'";
  67. $usuarios = mysql_query($query_usuarios, $conexion) or die(mysql_error());
  68. $row_usuarios = mysql_fetch_assoc($usuarios);
  69. $totalRows_usuarios = mysql_num_rows($usuarios);
  70.  
  71.  
  72. mysql_select_db($database_conexion, $conexion);
  73. $query_Recordset2 = "SELECT * FROM dir_usuarios where id_aviso='$id_aviso' AND NUM_DIR='1'";
  74. $Recordset2 = mysql_query($query_Recordset2, $conexion) or die(mysql_error());
  75. $row_Recordset2 = mysql_fetch_assoc($Recordset2);
  76. $totalRows_Recordset2 = mysql_num_rows($Recordset2);
  77.  
  78.  
  79. ?>
  #27 (permalink)  
Antiguo 22/01/2013, 15:03
Avatar de GatorV
$this->role('moderador');
 
Fecha de Ingreso: mayo-2006
Ubicación: /home/ams/
Mensajes: 38.567
Antigüedad: 17 años, 11 meses
Puntos: 2135
Respuesta: Consulta de Campos en tablas.

Pues con ese código solamente estas haciendo un echo del INSERT, tienes que realizar el insert usando mysql_query, no solamente imprimirlo en pantalla...
  #28 (permalink)  
Antiguo 22/01/2013, 15:26
Avatar de satjaen  
Fecha de Ingreso: septiembre-2012
Ubicación: Jaén (Andalucía)
Mensajes: 893
Antigüedad: 11 años, 7 meses
Puntos: 10
Respuesta: Consulta de Campos en tablas.

Cita:
Iniciado por GatorV Ver Mensaje
Pues con ese código solamente estas haciendo un echo del INSERT, tienes que realizar el insert usando mysql_query, no solamente imprimirlo en pantalla...
Perdona, asi tampoco me funciona:
Código PHP:
Ver original
  1. <?php require_once('Connections/conexion.php'); ?>
  2. <?php
  3. if (!function_exists("GetSQLValueString")) {
  4. function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
  5. {
  6.   if (PHP_VERSION < 6) {
  7.     $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  8.   }
  9.  
  10.   $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
  11.  
  12.   switch ($theType) {
  13.     case "text":
  14.       $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
  15.       break;    
  16.     case "long":
  17.     case "int":
  18.       $theValue = ($theValue != "") ? intval($theValue) : "NULL";
  19.       break;
  20.     case "double":
  21.       $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
  22.       break;
  23.     case "date":
  24.       $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
  25.       break;
  26.     case "defined":
  27.       $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
  28.       break;
  29.   }
  30.   return $theValue;
  31. }
  32. }
  33.  
  34. $editFormAction = $_SERVER['PHP_SELF'];
  35. if (isset($_SERVER['QUERY_STRING'])) {
  36.   $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
  37. }
  38. $id_aviso=$_POST['id_aviso'];
  39. $localidad = $_POST['localidad'];
  40. $provincia= $_POST['provincia'];
  41. $calle = $_POST['calle'];
  42. $numero = $_POST['numero'];
  43. $edificio= $_POST['edificio'];
  44. $puerta = $_POST['puerta'];
  45. $piso = $_POST['piso'];
  46. $letra= $_POST['letra'];
  47. $email = $_POST['email'];
  48. $NUM_DIR = $_POST['NUM_DIR'];
  49. $total = count($localidad);
  50. for ($i = 0; $i < $total; $i++) {
  51.         $local = $localidad[$i];
  52.         $prov = $provincia[$i];
  53.         $call = $calle[$i];
  54.         $nume = $numero[$i];
  55.         $edif = $edificio[$i];
  56.         $puer = $puerta[$i];
  57.         $pis = $piso[$i];
  58.         $letr = $letra[$i];
  59.         $emai = $email[$i];
  60.  
  61.    $insertSQL = sprintf("INSERT INTO dir_usuarios (local,prov,call,nume,edif,puer,pis,letr,emai,id_aviso,NUM_DIR)VALUES (%s, %s, %s, %s, %s, %s,%s, %s, %s, %s, %s)",
  62.                        GetSQLValueString($_POST['local'], "text"),
  63.                        GetSQLValueString($_POST['prov'], "text"),
  64.                        GetSQLValueString($_POST['call'], "text"),
  65.                        GetSQLValueString($_POST['nume'], "text"),
  66.                        GetSQLValueString($_POST['edif'], "text"),
  67.                        GetSQLValueString($_POST['puer'], "text"),
  68.                        GetSQLValueString($_POST['pis'], "text"),
  69.                        GetSQLValueString($_POST['letr'], "text"),
  70.                        GetSQLValueString($_POST['emai'], "text"),
  71.                        GetSQLValueString($_POST['id_aviso'], "int"),
  72.                        GetSQLValueString($_POST['NUM_DIR'], "text"));
  73.  
  74. mysql_select_db($database_conexion, $conexion);
  75. $usuarios = mysql_query($insertSQL, $conexion) or die(mysql_error());
  76. }
  77. mysql_select_db($database_conexion, $conexion);
  78. $query_usuarios = "SELECT *
  79. FROM usuarios WHERE id_aviso='$id_aviso'";
  80. $usuarios = mysql_query($query_usuarios, $conexion) or die(mysql_error());
  81. $row_usuarios = mysql_fetch_assoc($usuarios);
  82. $totalRows_usuarios = mysql_num_rows($usuarios);
  83.  
  84.  
  85. mysql_select_db($database_conexion, $conexion);
  86. $query_Recordset2 = "SELECT * FROM dir_usuarios where id_aviso='$id_aviso' AND NUM_DIR='1'";
  87. $Recordset2 = mysql_query($query_Recordset2, $conexion) or die(mysql_error());
  88. $row_Recordset2 = mysql_fetch_assoc($Recordset2);
  89. $totalRows_Recordset2 = mysql_num_rows($Recordset2);
  90.  
  91.  
  92. ?>

Me da error:
Código HTML:
Ver original
  1. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'call,nume,edif,puer,pis,letr,emai,id_aviso,NUM_DIR)VALUES (NULL, NULL, NULL, NUL' at line 1

Última edición por satjaen; 22/01/2013 a las 15:34
  #29 (permalink)  
Antiguo 22/01/2013, 15:57
Avatar de GatorV
$this->role('moderador');
 
Fecha de Ingreso: mayo-2006
Ubicación: /home/ams/
Mensajes: 38.567
Antigüedad: 17 años, 11 meses
Puntos: 2135
Respuesta: Consulta de Campos en tablas.

Pues no porque estas leyendo los valores de $_POST, como $_POST['local'] etc, tienes que usar las variables locales que estas definiendo dentro del for...

Me pregunto si realmente estas tratando de entender lo que te estoy explicando, o solo haciendo copy/paste sin entender...
  #30 (permalink)  
Antiguo 22/01/2013, 17:07
Avatar de satjaen  
Fecha de Ingreso: septiembre-2012
Ubicación: Jaén (Andalucía)
Mensajes: 893
Antigüedad: 11 años, 7 meses
Puntos: 10
Respuesta: Consulta de Campos en tablas.

Cita:
Iniciado por GatorV Ver Mensaje
Pues no porque estas leyendo los valores de $_POST, como $_POST['local'] etc, tienes que usar las variables locales que estas definiendo dentro del for...

Me pregunto si realmente estas tratando de entender lo que te estoy explicando, o solo haciendo copy/paste sin entender...
Vuelve a perdonarme, ya te avise que era novato. Esto queda tal que así:

Código HTML:
Ver original
  1. <div id="TabbedPanels2" class="TabbedPanels">
  2.             <ul class="TabbedPanelsTabGroup">
  3.               <li class="TabbedPanelsTab" tabindex="0">Dirección</li>
  4.               <li class="TabbedPanelsTab" tabindex="0">Dirección 2</li>
  5.               <li class="TabbedPanelsTab" tabindex="0">Dirección 3</li>
  6.             </ul>
  7.             <div class="TabbedPanelsContentGroup">
  8.               <div class="TabbedPanelsContent">
  9.               <table width="100%">
  10.                   <tr>
  11.                     <th scope="col">Calle</th>
  12.                     <input name="NUM_DIR[]" type="hidden" size="" value="1" />
  13.                     <th scope="col"><input name="calle[]" type="text" size="30"value="" /></th>
  14.                     <th scope="col"></th>
  15.                     <th scope="col"><input name="numero[]" type="text" size="1"value="" />
  16.                     EDF
  17.                     <input name="edificio[]" type="text" size="1"value="" />
  18.                     PTA
  19.                     <input name="puerta[]" type="text" size="1"value="" />
  20.                     PISO
  21.                     <input name="piso[]" type="text" size="1"value="" />
  22.                     LTA
  23.                     <input name="letra[]" type="text" size="1"value="" /></th>
  24.                 </tr>
  25.                      <tr>
  26.                     <th scope="col">Localidad</th>
  27.                     <th scope="col"><input name="localidad[]" type="text" size="30" value="" /></th>
  28.                     <th scope="col">Provincia</th>
  29.                     <th scope="col"><input name="provincia[]" type="text" size="30" value="" /></th>
  30.                   </tr>
  31.                   <tr>
  32.                   <th scope="col">Email</th>
  33.                     <th scope="col"><input name="email[]" type="text" size="30" value=""/></th>
  34.                 </tr>
  35.               </table>
  36.               </div>
  37.               <div class="TabbedPanelsContent">
  38.                <table width="100%">
  39.                   <tr>
  40.                     <th scope="col">Calle</th>
  41.                     <input name="NUM_DIR[]" type="hidden" size="" value="2" />
  42.                     <th scope="col"><input name="calle[]" type="text" size="30"value="" /></th>
  43.                     <th scope="col"></th>
  44.                     <th scope="col"><input name="numero[]" type="text" size="1"value="" />
  45.                     EDF
  46.                     <input name="edificio[]" type="text" size="1"value="" />
  47.                     PTA
  48.                     <input name="puerta[]" type="text" size="1"value="" />
  49.                     PISO
  50.                     <input name="piso[]" type="text" size="1"value="" />
  51.                     LTA
  52.                     <input name="letra[]" type="text" size="1"value="" /></th>
  53.                 </tr>
  54.                      <tr>
  55.                     <th scope="col">Localidad</th>
  56.                     <th scope="col"><input name="localidad[]" type="text" size="30" value="" /></th>
  57.                     <th scope="col">Provincia</th>
  58.                     <th scope="col"><input name="provincia[]" type="text" size="30" value="" /></th>
  59.                   </tr>
  60.                   <tr>
  61.                   <th scope="col">Email</th>
  62.                     <th scope="col"><input name="email[]" type="text" size="30" value=""/></th>
  63.                 </tr>
  64.               </table>
  65.               </div>
  66.               <div class="TabbedPanelsContent">
  67.                <table width="100%">
  68.                   <tr>
  69.                     <th scope="col">Calle</th>
  70.                     <input name="NUM_DIR[]" type="hidden" size="" value="3" />
  71.                     <th scope="col"><input name="calle[]" type="text" size="30"value="" /></th>
  72.                     <th scope="col"></th>
  73.                     <th scope="col"><input name="numero[]" type="text" size="1"value="" />
  74.                     EDF
  75.                     <input name="edificio[]" type="text" size="1"value="" />
  76.                     PTA
  77.                     <input name="puerta[]" type="text" size="1"value="" />
  78.                     PISO
  79.                     <input name="piso[]" type="text" size="1"value="" />
  80.                     LTA
  81.                     <input name="letra[]" type="text" size="1"value="" /></th>
  82.                 </tr>
  83.                      <tr>
  84.                     <th scope="col">Localidad</th>
  85.                     <th scope="col"><input name="localidad[]" type="text" size="30" value="" /></th>
  86.                     <th scope="col">Provincia</th>
  87.                     <th scope="col"><input name="provincia[]" type="text" size="30" value="" /></th>
  88.                   </tr>
  89.                   <tr>
  90.                   <th scope="col">Email</th>
  91.                     <th scope="col"><input name="email[]" type="text" size="30" value=""/></th>
  92.                 </tr>
  93.               </table>
  94.               </div>


Código PHP:
Ver original
  1. }
  2. $id_aviso=$_POST['id_aviso'];
  3. $localidad = $_POST['localidad'];
  4. $provincia= $_POST['provincia'];
  5. $calle = $_POST['calle'];
  6. $numero = $_POST['numero'];
  7. $edificio= $_POST['edificio'];
  8. $puerta = $_POST['puerta'];
  9. $piso = $_POST['piso'];
  10. $letra= $_POST['letra'];
  11. $email = $_POST['email'];
  12. $NUM_DIR = $_POST['NUM_DIR'];
  13. $total = count($localidad);
  14. for ($i = 0; $i < $total; $i++) {
  15.         $local = $localidad[$i];
  16.         $prov = $provincia[$i];
  17.         $call = $calle[$i];
  18.         $nume = $numero[$i];
  19.         $edif = $edificio[$i];
  20.         $puer = $puerta[$i];
  21.         $pis = $piso[$i];
  22.         $letr = $letra[$i];
  23.         $emai = $email[$i];
  24.         $NUM_D = $NUM_DIR[$i];
  25.  
  26.    $insertSQL = sprintf("INSERT INTO dir_usuarios (localidad,provincia,calle,numero,edificio,puerta,piso,letra,email,id_aviso,NUM_DIR)VALUES ('$local', '$prov', '$call', '$nume', '$edif','$puer','$pis','$letr', '$emai', '$id_aviso', '$NUM_D')",
  27.                        GetSQLValueString($_POST['localidad'], "text"),
  28.                        GetSQLValueString($_POST['provincia'], "text"),
  29.                        GetSQLValueString($_POST['calle'], "text"),
  30.                        GetSQLValueString($_POST['numero'], "text"),
  31.                        GetSQLValueString($_POST['edificio'], "text"),
  32.                        GetSQLValueString($_POST['puerta'], "text"),
  33.                        GetSQLValueString($_POST['piso'], "text"),
  34.                        GetSQLValueString($_POST['letra'], "text"),
  35.                        GetSQLValueString($_POST['email'], "text"),
  36.                        GetSQLValueString($_POST['id_aviso'], "int"),
  37.                        GetSQLValueString($_POST['NUM_DIR'], "int"));
  38.  
  39. mysql_select_db($database_conexion, $conexion);
  40. $dir_usuarios = mysql_query($insertSQL, $conexion) or die(mysql_error());
  41. }


Una pregunta que quiero hacerte es que en un primer formulario que tengo donde ingreso los datos del cliente y luego recupero esos datos con un id autoincrement y vuelvo a ingresar los datos de la dirección con dicho id. Habitualmente se hace así o puedo hacerlo de otra forma mas rápida. Siempre tengo que hacer un INSERT detras de otro para poder sacar el id que las une no

PD:Gracias por no haberme hecho el código desde el principio

Etiquetas: campos, formulario, tabla
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 23:27.