Tema: txt a mysql
Ver Mensaje Individual
  #1 (permalink)  
Antiguo 31/12/2013, 03:02
Avatar de totti026
totti026
 
Fecha de Ingreso: junio-2011
Mensajes: 150
Antigüedad: 12 años, 10 meses
Puntos: 4
Pregunta txt a mysql

hola que tal,
tengo el siguiente código que inserta un txt a una base de datos:
Código PHP:
Ver original
  1. <?php
  2.    
  3. $conexion = new mysqli("localhost", "root", "", "txt", "3306");
  4.            
  5. $lineas = file('articulos.txt');
  6.            
  7. foreach ($lineas as $linea_num => $linea)
  8. {
  9.     $datos = explode("\t",$linea);
  10.  
  11.     $clave = trim($datos[0]);
  12.     $producto = trim($datos[1]);
  13.     $precio = trim($datos[2]);
  14.  
  15.     $consulta = "INSERT INTO tblproducto(clave,producto,precio) VALUES('$clave','$producto',$precio)";
  16.     $conexion->query($consulta);
  17. }
  18.  
  19. ?>

este es el txt:
Código TXT:
Ver original
  1. A001    ADAPTADOR DE AUDIO USB  120.00
  2. A002    ADAPTADOR DE RED INALAMBRICO    149.00
  3. A003    MONITOR LED LG 18.5"    1699.00
  4. A004    SERVIDOR LINUX 5TB 30GB RAM 27000.00
  5. A005    SOFTWARE    2000.00

Pero si en el txt no tiene en alguna fila algun dato ya no lo guarda...
osea que si el txt lo tengo asi:
Código TXT:
Ver original
  1. A001    ADAPTADOR DE AUDIO USB  120.00
  2. A002    ADAPTADOR DE RED INALAMBRICO    149.00
  3. A003    MONITOR LED LG 18.5"    1699.00
  4. A004    SERVIDOR LINUX 5TB 30GB RAM 27000.00
  5. A005    SOFTWARE    2000.00
  6.     IMPRESORA   17500.00
  7.         50.00
  8. A008    LAPTOP  6400.00
  9. A009
  10. A010    TECLADO

¿como se podría hacer para que si se guardara?
(son tabulaciones las separaciones)

Saludos y gracias