Foros del Web » Programando para Internet » PHP »

Como meter un archivo csv a una BD

Estas en el tema de Como meter un archivo csv a una BD en el foro de PHP en Foros del Web. hola: Espero que me puedan ayudar, bueno lo que quiero hacer es que me diante un chivo de excel con extencion csv y presionar un ...
  #1 (permalink)  
Antiguo 02/04/2007, 11:08
 
Fecha de Ingreso: marzo-2007
Mensajes: 26
Antigüedad: 17 años, 1 mes
Puntos: 0
Pregunta Como meter un archivo csv a una BD

hola:

Espero que me puedan ayudar, bueno lo que quiero hacer es que me diante un chivo de excel con extencion csv y presionar un boton me suba toda la informacion a la base de datos o a sus diferentes tablas de esa BD.
  #2 (permalink)  
Antiguo 03/04/2007, 07:52
Avatar de HMSJUAN  
Fecha de Ingreso: febrero-2007
Mensajes: 54
Antigüedad: 17 años, 2 meses
Puntos: 1
Re: Como meter un archivo csv a una BD

Oye Esto No Lo He Probado Lo Encontre asi y Asi mismo lo envio........


Espero Que Si Lo modificas y funciona lo comparta con nosotros...


Upload Csv Insert To MySql

Hi all

On this tutorial or example , i want to explain
add data from csv or txt to mysql using fgetcsv

first ofcourse we need csv file
this is the example

example.csv
indra,[email protected]
dadan,[email protected]

now we need table in mysql
table: email
id | name | email

this the structure
after that we need to create our php file

insert_cvs_mysql.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>
";
}
?>
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 22:56.