Foros del Web » Programando para Internet » PHP »

Archivo no se guarda correctamente en BD.

Estas en el tema de Archivo no se guarda correctamente en BD. en el foro de PHP en Foros del Web. Hola a todos. Tengo un script en PHP que sube un archivo CSV (Excel) y lo inserta en la BD, pero el problema es que ...
  #1 (permalink)  
Antiguo 25/06/2009, 08:28
 
Fecha de Ingreso: junio-2009
Mensajes: 250
Antigüedad: 14 años, 10 meses
Puntos: 1
Archivo no se guarda correctamente en BD.

Hola a todos.

Tengo un script en PHP que sube un archivo CSV (Excel) y lo inserta en la BD, pero el problema es que lo inserta así:

Código PHP:
Tabla'personas'

NOMBRE         DNI          (Campos)

PEDRO,789                    (Valores)         
JUAN,548
MARIA
,123 
Cuando debería ser así:

Código PHP:
NOMBRE          DNI          (Campos)

PEDRO            789          (Valores)
JUAN               548
MARIA             123 
Además de que cuando ejecuto el script me marca algunos errores como éstos, ésto es ya después de subir los archivos, como mencioné si me los inserta en la BD, pero no correctamente:

Código PHP:
NoticeUndefined variableuploaddir in C:wampwwwsitiopruebacsv2.php on line 17
File sucessfully uploaded to Libro2
.csv

Notice
fgetcsv() [function.fgetcsv]: delimiter must be a single character in C:wampwwwsitiopruebacsv2.php on line 24

Notice
Undefined offset1 in C:wampwwwsitiopruebacsv2.php on line 26
INSERT into personas
(NOMBRE,DNIvalues('NOMBRE,DNI','')

Noticefgetcsv() [function.fgetcsv]: delimiter must be a single character in C:wampwwwsitiopruebacsv2.php on line 24

Notice
Undefined offset1 in C:wampwwwsitiopruebacsv2.php on line 26
INSERT into personas
(NOMBRE,DNIvalues('MARIA,123','')

Noticefgetcsv() [function.fgetcsv]: delimiter must be a single character in C:wampwwwsitiopruebacsv2.php on line 24

Notice
Undefined offset1 in C:wampwwwsitiopruebacsv2.php on line 26
INSERT into personas
(NOMBRE,DNIvalues('JOSE,456','')

Noticefgetcsv() [function.fgetcsv]: delimiter must be a single character in C:wampwwwsitiopruebacsv2.php on line 24

Notice
Undefined offset1 in C:wampwwwsitiopruebacsv2.php on line 26
INSERT into personas
(NOMBRE,DNIvalues('PEDRO,789','')

Noticefgetcsv() [function.fgetcsv]: delimiter must be a single character in C:wampwwwsitiopruebacsv2.php on line 24
Import done 
También cabe la posibilidad de que mi archivo CSV pueda estar mal... cuando lo abro con bloc de notas está así:

Código PHP:
NOMBRE,DNI
MARIA
,123
JOSE
,456
PEDRO
,789 
SCRIPT CORREGIDO, FUNCIONA:

Código PHP:
Ver original
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  5. <title>Documento sin t&iacute;tulo</title>
  6. </head>
  7.  
  8. <body>
  9. <?php
  10. require_once('Connections/conex.php');
  11. mysql_select_db($database_conex, $conex);
  12. //DbOpen();
  13. echo '<form action="' . $_SERVER['PHP_SELF'] . '" method="post" enctype="multipart/form-data">';
  14. if(isset($_POST['submit'])) {
  15.  
  16. if (move_uploaded_file($_FILES['filename']['tmp_name'], "Libro2.csv")) {
  17. echo "File sucessfully uploaded to " . $uploaddir . basename($_FILES['filename']['name']) . "<BR>";
  18. $filename = "Libro2.csv";
  19. $handle = fopen($filename, 'r');
  20. } else {
  21. echo "Error : " . $uploaddir . basename($_FILES['filename']['name'])  . '\n' . $_FILES['filename']['error'] . "<BR>";
  22. }
  23.  
  24. while (($data = fgetcsv($handle, 4096, ',')) !== FALSE) {
  25. $data = str_replace("'","''",$data);
  26. $import="INSERT into personas(NOMBRE,DNI) values('$data[0]','$data[1]')";
  27. $runq = mysql_query($import) or die(mysql_error());
  28. echo $import;
  29. echo "<br>";
  30. }
  31. fclose($handle);
  32. print "Import done";
  33.  
  34. }
  35. else {
  36. print "Type file name to import:<br>";
  37. print "<input type='file' name='filename' size='20'><br>";
  38. print "<input type='submit' name='submit' value='submit'>";
  39. }
  40. $import="select * from personas";
  41. $runq = mysql_query($import) or die(mysql_error());
  42.  
  43. //DbClose();
  44. ?>

Espero puedan ayudarme, se los agradezco de antemano.

Saludos.

EDIT: Ya lo resolví, sólo corrijo el script en php por si le sirve a alguien en un futuro.

Saludos.

Última edición por Gaug; 25/06/2009 a las 10:14
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 13:07.