Foros del Web » Programando para Internet » PHP »

importar csv a mysql

Estas en el tema de importar csv a mysql en el foro de PHP en Foros del Web. hola amigos tengo un script que lo que hace es importar un archivo csv a una tabla mysql pero me bota un error Query failed: ...
  #1 (permalink)  
Antiguo 25/09/2008, 14:47
(Desactivado)
 
Fecha de Ingreso: mayo-2008
Mensajes: 67
Antigüedad: 15 años, 10 meses
Puntos: 0
importar csv a mysql

hola amigos tengo un script que lo que hace es importar un archivo csv a una tabla mysql pero me bota un error

Query failed: Column count doesn't match value count at row 1

mi pregunta es donde esta el errror espero alguien me ayude.
Código PHP:
<?php
//First we need to define our user pass and database name
$tbl "yourtablename";
$host="yourdatabasehost";
$user="youruser";
$pass="yourpass";
$database="yourdatabase";

//we need function to get the csv
function getcsv($filename$delim =","){

$row 0;
$dump = array(); //create new array for hold the data csv

$f fopen ($filename,"r");
$size filesize($filename)+1;
while (
$data fgetcsv($f$size$delim)) {
$dump[$row] = $data//put the data to array
//echo $data[1]."<br>";
$row++;
}
fclose ($f);

return 
$dump;
}

//this function for insert data to csv
function makeINSERTS($text$table){
global 
$linkdata//make global database connection
$insert = array(); //make array for hold data insert
$i 0;

while (list(
$key$val) = @each($text)){
//insert the data
$insert[$i] = "INSERT into ".$table." VALUES('','";
$insert[$i] .= implode("','"$val);
$insert[$i] .= "')\n";
$result mysql_query($insert[$i], $linkdata) or die('Query failed: ' mysql_error());
$i++;
}

return 
$insert;

}

//this verify is the file csv upload
if ($_POST["submit"]=="submit") {
if (
is_uploaded_file($_FILES['userfile']['tmp_name'])) {

$file=$_FILES['userfile']['tmp_name'];
$linkdata mysql_connect($host$user$pass)
or die(
'Could not connect: ' mysql_error());

mysql_select_db($database) or die('Could not select database');


$CSVarray getcsv($file);
$CSVarray makeINSERTS($CSVarray"$tbl");


} else {
echo 
"error upload file";
exit;
}


} else {
//form upload
echo "
<FORM ENCTYPE=\"multipart/form-data\" ACTION=\""
.$_SERVER['PHP_SELF']."\" METHOD=POST>
Upload this file: <INPUT NAME=\"userfile\" TYPE=\"file\">
<INPUT TYPE=\"submit\" VALUE=\"submit\" name=submit></FORM>
"
;
}
?>
  #2 (permalink)  
Antiguo 25/09/2008, 15:47
Avatar de Yedi  
Fecha de Ingreso: junio-2006
Ubicación: México
Mensajes: 159
Antigüedad: 17 años, 10 meses
Puntos: 2
Respuesta: importar csv a mysql

ese mensaje significa q algo esta mal en tu consulta...y recuerda q en la forma de pedir esta el dar
  #3 (permalink)  
Antiguo 25/09/2008, 16:07
Avatar de boby6killer  
Fecha de Ingreso: abril-2008
Mensajes: 81
Antigüedad: 16 años
Puntos: 1
Respuesta: importar csv a mysql

uuuuuyyyy amigo, cuenta bien los campos que estas ingresando (algo basico) y aprende a pedir las cosas!!!
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 02:41.